'use strict';
(function (exports) {
    var signal1 = res.signal1;
    var signal2 = res.signal2;
    var signal3 = res.signal3;
    var signal4 = res.signal4;
    var signal5 = res.signal5;

    var $ = null;

    var direction = ["东", "南", "西", "北"];

    var PAINAMES = ["", "一万", "二万", "三万", "四万", "五万", "六万", "七万", "八万", "九万", "一条", "二条", "三条", "四条", "五条", "六条", "七条", "八条", "九条", "一筒", "二筒", "三筒", "四筒", "五筒", "六筒", "七筒", "八筒", "九筒"];

    // CONST
    var FAPAI_ANIM_DELAY = 0.04;
    var FAPAI_ANIM_DURATION = 0.2;
    var MOPAI_ANIM_DURATION = 0.3;
    var UPDOWNPAI_ANIM_DURATION = 0.088;
    var UPPAI_Y = 30;
    var CHUPAI_MID_NODE_SCALE_MAP = {0: 1.2, 2: 1.2, 1: 1.2, 3: 1.2};

    var MAP2PNUM = {1: 4, 2: 3};

    var OP_CHI = 1;
    var OP_PENG = 2;
    var OP_GANG = 3;
    var OP_HU = 4;
    var OP_PASS = 5;
    var OP_TING = 6;
    var OP_LIANG = 7;
    var OP_KAIGANG = 50;
    var OP_XIAOHU = 51;

    var ROOM_STATE_CREATED = 1;
    var ROOM_STATE_ONGOING = 2;
    var ROOM_STATE_ENDED = 3;

    //定义一些页面层级
    var jiesuanZorder = 2;
    var zongjiesuanZorder = 2;
    var dingweiZorder = 1;

    var posConf = {
        headPosBak: {}
        , paiA0PosBak: {}
        , paiA0ScaleBak: {}
        , paiADistance: []
        , paiALiangDistance: [0.5, 3.5, 0, -3.5]
        , paiMopaiDistance: {0: 16, 1: 40, 2: 30, 3: 40}
        , paiGDistance: []
        , paiUsedDistance: []
        , paiUsedZOrder: {
            0: {0: -1, 10: 0, 20: 1, 30: 2, 40: 3, 50: 4, 60: 5}
            , 1: {0: -1, 10: -1, 20: -1, 30: -1, 40: -1, 50: -1, 60: -1}
            , 2: {0: 0, 10: -1, 20: -2, 30: -3, 40: -4, 50:-5, 60: -6}
            , 3: {0: 0, 10: 1, 20: 2, 30: 3, 40: 4, 50: 5, 60: 6}
        }
        , cpghgPositionX: {
            2: [660, 920], 3: [425, 700, 960], 4: [320, 580, 840, 1070],
            5: [210, 460, 700, 940, 1130], 6: [210, 460, 700, 940, {x: 700, y: 360}, 1180]
        }
        , upPaiPositionY: null
        , downPaiPositionY: null
        , groupWidth: {}
        , groupHeight: {}
        , groupDistance: {0: 4, 1: 4, 2: 16, 3: 4}
        , groupToFirstPaiDistance: {0: 10, 1: -14, 2: 26, 3: -14}

        , paiPos: {}

        , ltqpPos: {}
        , ltqpRect: {}
        , ltqpCapInsets: {
            0: cc.rect(26, 31, 1, 1)
            , 1: cc.rect(26, 31, 1, 1)
            , 2: cc.rect(44, 25, 1, 1)
            , 3: cc.rect(42, 26, 1, 1)
        }
        , ltqpEmojiPos: {
            0: cc.p(40, 28)
            , 1: cc.p(39, 28)
            , 2: cc.p(40, 40)
            , 3: cc.p(56, 28)
        }
        , ltqpVoicePos: {
            0: cc.p(40, 28)
            , 1: cc.p(37, 28)
            , 2: cc.p(42, 40)
            , 3: cc.p(58, 28)
        }
        , ltqpEmojiSize: {}
        , ltqpTextDelta: {
            0: cc.p(0, -4)
            , 1: cc.p(-7, 2)
            , 2: cc.p(-1, 9)
            , 3: cc.p(8, 3)
        }
        , gPaiPos: {
            0: {1: 0, 2: 1, 3: 2},
            1: {0: 2, 2: 0, 3: 1},
            2: {0: 1, 3: 0, 1: 2},
            3: {0: 0, 1: 1, 2: 2}
        }
        //方向指标的角度
        , huFromAngle: [
            180,
            270,
            0,
            90
        ]
        //方向指标的位置
        , fromPos: [
            {x: 20, y: 80},
            {x: 50, y: 35},
            {x: 20, y: 20},
            {x: 20, y: 35}
        ]
    };

    var data;
    var isReconnect;

    var roomState = null;
    var mapId = 2;
    var playerNum = 4;
    var rowBegin = 1;

    var enableChupaiCnt = 0;

    var isCCSLoadFinished = false;

    var uid2position = {};
    var uid2playerInfo = {};
    var position2uid = {};
    var position2sex = {};
    var position2playerArrIdx = {};

    var hasChupai = false;

    var turnRow = null;

    var leftPaiCnt = 0;

    var isReplay = null;

    var forRows = null;

    var lianging = false;

    var liangPai = 0;
    var liangPaiIdx = 0;
    var liangStep = 0;

    var hideGangStep = 0;
    var hideGangArr = [];
    var hidedGangArr = [];
    var hideGangChupaiArr = [];

    var afterLianging = false;

    var disabledChuPaiIdMap = {};

    var disableChupai = false;

    var selectingXiaohu = false;

    var curHuPaiId = 0;

    var laiziPaiId = 0;

    // for wuhan
    var laizipiPaiAId = 0;
    var laizipiPaiBId = 0;

    //for taiyuan lisi
    var lisiPaiCountList = {
        0: 0,
        1: 0,
        2: 0,
        3: 0
    };
    var lisiPaiArr = {
        0: [],
        1: [],
        2: [],
        3: []
    };

    var quePaiList = {};
    var vibrate = function () {
        if (cc.sys.os == cc.sys.OS_IOS)
            cc.Device.vibrate(1);
        else if (cc.sys.os == cc.sys.OS_ANDROID) {
            cc.Device.vibrate(1);
        }
    };
    var clearVars = function () {
        roomState = null;
        mapId = gameData.mapId;

        playerNum = gameData.playerNum || MAP2PNUM[gameData.appId] || 4;
        rowBegin = 1;
        isCCSLoadFinished = false;
        uid2position = {};
        uid2playerInfo = {};
        position2uid = {};
        position2sex = {};
        position2playerArrIdx = {};
        hasChupai = false;
        turnRow = null;
        leftPaiCnt = 0;
        isReplay = null;
        forRows = null;
        lianging = false;
        liangPai = 0;
        liangPaiIdx = 0;
        liangStep = 0;
        hideGangStep = 0;
        hideGangArr = [];
        hidedGangArr = [];
        hideGangChupaiArr = [];
        afterLianging = false;
        disabledChuPaiIdMap = {};
        disableChupai = false;
        selectingXiaohu = false;
        enableChupaiCnt = 0;

        curHuPaiId = 0;

        laiziPaiId = 0;
        laizipiPaiAId = 0;
        laizipiPaiBId = 0;

        lisiPaiCountList = {
            0: 0,
            1: 0,
            2: 0,
            3: 0
        };
        lisiPaiArr = {
            0: [],
            1: [],
            2: [],
            3: []
        };
        quePaiList = {};

    };

    var getPaiNameByRowAndId = function (row, id, isLittle, isStand) {
        var prefix;

        var ret = null;

        if (row == 0 && isLittle) prefix = "p0s";
        if (row == 0 && !isLittle) prefix = "p2l";
        if (row == 1 && isLittle) prefix = "p1s";
        if (row == 1 && !isLittle) prefix = "p2l";
        if (row == 2 && isLittle) prefix = "p2s";
        if (row == 2 && !isLittle) prefix = "p2l";
        if (row == 3 && isLittle) prefix = "p3s";
        if (row == 3 && !isLittle) prefix = "p2l";

        if (prefix) ret = prefix + id + '.png';

        if (row == 0 && id == 0 && isLittle) ret = 'p0s0' + '.png';
        if (row == 1 && id == 0 && isLittle) ret = 'p1s0' + '.png';
        //if (row == 2 && id == 0 && isLittle) ret = '' + '.png';
        if (row == 3 && id == 0 && isLittle) ret = 'p3s0' + '.png';
        //if (row == 0 && id == 0 && !isLittle) ret = 'bs' + '.png';
        //if (row == 1 && id == 0 && !isLittle) ret = 'bh' + '.png';
        if (row == 2 && id == 0 && !isLittle) ret = 'p2l0' + '.png';
        //if (row == 3 && id == 0 && !isLittle) ret = 'bh' + '.png';

        if (row == 0 && isStand && isLittle) ret = 'h0s' + '.png';
        if (row == 1 && isStand && isLittle) ret = 'h1s' + '.png';
        if (row == 3 && isStand && isLittle) ret = 'h3s' + '.png';
        if (row == 0 && isStand && !isLittle) ret = 'p2l0' + '.png';
        if (row == 1 && isStand && !isLittle) ret = 'by' + '.png';
        if (row == 3 && isStand && !isLittle) ret = 'bz' + '.png';

        return ret;
    };

    var playAnimSceneCall = function (distNode, res, posx, posy, loop, func) {
        var cacheNode = distNode.getChildByName(res);
        if (!cacheNode) {
            var animScene = ccs.load(res);
            cacheNode = animScene.node;
            if (cacheNode) {
                cacheNode.setName(res);
                distNode.addChild(cacheNode);
                cacheNode.runAction(animScene.action);
                cacheNode.setPosition(posx, posy);

                var userdata = cacheNode.getUserData() || {};
                userdata.action = animScene.action;
                cacheNode.setUserData(userdata);
            } else {
                showAlert('load ' + res + " failed");
            }
        }

        var userdata = cacheNode.getUserData();
        userdata.action.play('action', loop);
        if (func) {
            userdata.action.setLastFrameCallFunc(func);
        }
        return cacheNode;
    };

    var MaLayer = cc.Layer.extend({
        chatLayer: null,
        chiLayer: null,
        settingsLayer: null,
        throwDiceLayer: null,
        kaigangLayer: null,
        beforeOnCCSLoadFinish: null,
        afterGameStart: null,
        isIconMove: false,
        onEnter: function () {
            cc.Layer.prototype.onEnter.call(this);
        },
        getRootNode: function () {
            return this.getChildByName("Scene");
        },
        initExtraMapData: function (data) {
            var that = this;
        },
        onCCSLoadFinish: function () {
            var that = this;
            addCachedCCSChildrenTo(res.MaScene1_json, this);
            if (gameData.mapId == MAP_ID_SHANXI_LLFY) {
                addCachedCCSChildrenTo(res.DingPanel_json, this);
            }
            batchSetChildrenZorder(this.getRootNode(), {
                info0: 1, info1: 1, info2: 1, info3: 1,
                info_n0: 2, info_n1: 2, info_n2: 20, info_n3: 2,
                row2: 10,
                cpghg: 12,
                dingPanel: 100,
                btn_bg: 30, setting: 31,location_btn_red:31,btn_changeTable:31, chat: 29, jiesan: 31, btn_control_btns: 32,
                xuanpao_back: 100
            });

            $ = create$(this.getRootNode());
            if (gameData.mapId == MAP_ID_SHANXI_LLFY) {
                $('dingPanel').setVisible(false);
            }

            this.getPai = this.getPai();
            this.getGPai = this.getGPai();
            this.addUsedPai = this.addUsedPai();
            this.countDown = this.countDown();

            this.calcPosConf();


            MicLayer.init($('btn_mic'), this);

            if (isReconnect) {
                gameData.zhuangUid = data['zhuang_uid'];
                gameData.lianzhuang = data['lianzhuang'];
                gameData.leftRound = data['left_round'];
                gameData.players = data['players'];
                gameData.wanfaDesp = data['desp'];
                gameData.totalRound = data['total_round'];

                var status = data['room_status'] || ROOM_STATE_ONGOING;
                if (status == ROOM_STATE_ENDED) {
                    this.setRoomState(ROOM_STATE_ONGOING);
                    this.setRoomState(ROOM_STATE_ENDED);
                }
                else {
                    this.setRoomState(status);
                }
                this.initExtraMapData(data);
            }
            else
                this.setRoomState(ROOM_STATE_CREATED);

            this.clearTable4StartGame(isReconnect, isReconnect, data);

            this.startTime();

            this.startSignal();
            safeHide($('btn_test'));
            TouchUtil.setOnclickListener($('chat'), function () {
                showWindow('ChatWindow');
            });

            TouchUtil.setOnclickListener($('btn_changeTable'),function () {
                var curBackId = cc.sys.localStorage.getItem('newTableBackKEY') || 0;
                var _tempBg = null;
                if(parseInt(curBackId) < 5){
                    _tempBg = res['table_back' + (parseInt(curBackId) + 1)];
                    $('bg').setTexture(_tempBg);
                    cc.sys.localStorage.setItem('newTableBackKEY', (parseInt(curBackId)+1));
                }else{
                    $('bg').setTexture(res['table_back0']);
                    cc.sys.localStorage.setItem('newTableBackKEY', 0);
                }
            });

            TouchUtil.setOnclickListener($('setting'), function () {
                showWindow('SettingWindow',Setting_TYPE.Table_dou14);
            });
            TouchUtil.setOnclickListener($('btn_yxks'), function () {
                network.send(3004, {room_id: gameData.roomId});
            });
            TouchUtil.setOnclickListener($('btn_liang'), function () {
                $('dingPanel').setVisible(true);
            });

            TouchUtil.setOnclickListener($('wanfaPanel'), function () {
                $('wanfaPanel').setVisible(false);
            });

            TouchUtil.setOntouchListener($('btn_wanfa'), function () {
                that.showWanfaLayer();
            });

            if (mapId == MAP_ID_SHANXI_LLFY) {
                TouchUtil.setOnclickListener($('dingPanel.btn_close'), function () {
                    $('dingPanel').setVisible(false);
                });
            }

            TouchUtil.setOnclickListener($('btn_zhunbei'), function () {
                network.send(3004, {room_id: gameData.roomId});
            });

            TouchUtil.setOnclickListener($('btn_invite'), function () {
                ShareUtil.ShareRoomId(gameData.roomId, gameData.wanfaDesp, gameData.playerNum, gameData.players.length);
            });

            TouchUtil.setOnclickListener($('btn_fanhui'), function () {
                    showMessageBox('确定要退出房间吗?', function () {
                        network.send(3003, {room_id: gameData.roomId});
                    }, null, false, true, true);
            });

            TouchUtil.setOnclickListener($('jiesan'), function () {
                if (that.getRoomState() == ROOM_STATE_CREATED) {
                    if (window.inReview)
                        network.send(3003, {room_id: gameData.roomId});
                    else
                        showMessageBox('解散房间不扣房卡，是否确定解散？', function () {
                            network.send(3003, {room_id: gameData.roomId});
                        }, null, false, false, true, true);
                    return;
                }

                showMessageBox('确定要申请解散房间吗？', function () {
                    network.send(3009, {room_id: gameData.roomId, is_accept: 1});
                }, null, true, false, true, true);
            });

            TouchUtil.setOnclickListener($('btn_jiesan'), function () {
                if (window.inReview)
                    network.send(3003, {room_id: gameData.roomId});
                else
                    var is_club = gameData['is_club'];
                    showMessageBox('解散房间不扣房卡，是否确定解散？', function () {
                        if(is_club){
                            network.send(3003, {daikai_room_id: gameData.roomId, force: true});
                        }else{
                            network.send(3003, {room_id: gameData.roomId});
                        }
                    }, null, false, false, true, true);
            });

            TouchUtil.setOnclickListener($('btn_control_btns'), function () {
                that.changeBtnStatus();
            });
            TouchUtil.setOnclickListener($('bgPanel'), function () {
                that.hideControlBtns();
            });

            TouchUtil.setOnclickListener($('info0'), function () {
                that.showPlayerInfoPanel(0);
            });
            TouchUtil.setOnclickListener($('info1'), function () {
                that.showPlayerInfoPanel(1);
            });
            TouchUtil.setOnclickListener($('info2'), function () {
                that.showPlayerInfoPanel(2);
            });
            TouchUtil.setOnclickListener($('info3'), function () {
                that.showPlayerInfoPanel(3);
            });
            TouchUtil.setOnclickListener($('info0.ti'), function () {
                that.tiAlert(position2uid[0]);
            });
            TouchUtil.setOnclickListener($('info1.ti'), function () {
                that.tiAlert(position2uid[1]);
            });
            TouchUtil.setOnclickListener($('info2.ti'), function () {
            });
            TouchUtil.setOnclickListener($('info3.ti'), function () {
                that.tiAlert(position2uid[3]);
            });

            TouchUtil.setOnclickListener($("location_btn_red"), function () {
                showWindow('PlayerInfoLocationWindow', false, that);

            });
            // TouchUtil.setOnclickListener($("location_btn_green"), function () {
            //     showWindow('PlayerInfoLocationWindow', false, that);
            //
            // });
            // console.log("这里添加3002");
            this.addCustomListener(3002, function (data) {
                gameData.last3002 = data;
                gameData.playerNum = data['max_player_cnt'];
                if (isReplay) {
                    mapId = data['map_id'];
                    gameData.mapId = data['map_id'];
                    gameData.wanfaDesp = data['desp'];
                }
                if (that.getRoomState() == ROOM_STATE_CREATED) {
                    gameData.ownerUid = data['owner'];
                    gameData.players = data['players'];
                    that.onPlayerEnterExit();
                }
                gameData.daikaiPlayer = data['daikai_player'];
                gameData.roomClubId = data['options'] ? data['options']['club_id'] : null;
            });

            //不再用回放码
            this.addCustomListener(4201, function (data) {

            })
            this.addCustomListener(3003, function (data) {
                var uid = data['uid'];
                if (uid == gameData.uid) {
                    // cc.director.runScene(new MainScene());
                    showBoard('HallBoard');
                    return;
                }
                if (that.getRoomState() == ROOM_STATE_ONGOING)
                    return;
                if (data['del_room']) {
                    var owner = uid2playerInfo[gameData.ownerUid];
                    if(owner){
                        showAlert('房主' + (owner ? owner.nickname : data['nickname'] || '') + '已解散房间', function () {
                            // cc.director.runScene(new MainScene());
                            showBoard('HallBoard');
                        });
                    }else{
                        showAlert('代开房主已解散房间', function () {
                            // cc.director.runScene(new MainScene());
                            showBoard('HallBoard');
                        })
                    }
                }
                else {
                    var uid = data['uid'];
                    _.remove(gameData.players, function (player) {
                        return player.uid == uid;
                    });
                    that.onPlayerEnterExit();
                }
            });
            this.addCustomListener(3004, function (data) {
                if (that.getRoomState() != ROOM_STATE_ENDED)
                    return;

                if(!data){
                    return;
                }
                var uid = data['uid'];
                that.setReady(uid);
            });
            this.addCustomListener(3013, function (data) {
                gameData.numOfCards = data['numof_cards'];
            });
            this.addCustomListener(3005, function (data) {
            });
            this.addCustomListener(4081, function (data) {
                that.showXuanPao();
                that.hideAllPlayerPao();
                for (var i = 0; i < 4; i++) {
                    var isxuanpao = new cc.Sprite(cc.textureCache.addImage(res.xiapaozhongh));
                    $('info' + i + ".isxuanpao").addChild(isxuanpao);
                }
            });
            //点炮状态
            this.addCustomListener(4083, function (data) {
                for (var key in data['sel_map']) {
                    console.log(data['sel_map'][key]);
                    var row = uid2position[parseInt(key)];
                    var paoNode = $('info' + row + ".isxuanpao");
                    paoNode.removeAllChildren();
                    if (data['sel_map'][key] == -1) {
                        var isxuanpao = new cc.Sprite(cc.textureCache.addImage(res.xiapaozhongh));
                        paoNode.addChild(isxuanpao);
                    }
                    else {
                        var isxuanpao = new cc.Sprite(cc.textureCache.addImage(res.yixiapaov));
                        paoNode.addChild(isxuanpao);
                    }
                }

            });
            this.addCustomListener(4082, function (data) {
                var xuanpao_back = $('xuanpao_back');
                xuanpao_back.setVisible(false);
                that.showPlayerPao(data['p_score']);
            });

            this.addCustomListener(3009, function (data,errorCode) {
                if (errorCode == -1) {
                    showAlert("长时间未开始,房间自动解散");
                    return
                }
                if (data["arr"] == null || data["arr"] == undefined || data["arr"] == '' || (data["arr"] && data["arr"].length == 0)) {
                    return;
                }
                var that = this;
                var arr = data['arr'];
                var leftSeconds = data['left_sec'];
                leftSeconds = (leftSeconds < 0 ? 0 : leftSeconds);
                var byUserId = data['arr'][0].uid;
                if (that.isJiesan)
                    return;
                that.isJiesan = data['is_jiesan'];

                if (!that.RoomQuitWindow) {
                    that.RoomQuitWindow = showWindow('RoomQuitWindow', leftSeconds, arr, byUserId, data);
                } else {
                    that.RoomQuitWindow.setData(leftSeconds, arr, byUserId, data);
                }
                that.RoomQuitWindow.setVisible(true);

            });
            this.addCustomListener(3200, (function () {
                var interval = null;
                return function (data) {
                    if (interval) {
                        clearInterval(interval);
                        interval = null;
                    }

                    var content = decodeURIComponent(data['content']);
                    var duration = (content.length * 0.3 <= 10) ? 10 : content.length * 0.3;
                    var totalDuration = (0.2 + duration + 0.3 + 2) * 1000;

                    var func = function () {
                        if (!that || !cc.sys.isObjectValid(that)) {
                            if (interval) {
                                clearInterval(interval);
                                interval = null;
                            }
                            return;
                        }
                        var speaker = $('speaker');
                        var speakerPanel = $('speaker.panel');
                        if (!cc.sys.isObjectValid(speakerPanel)) {
                            return;
                        }
                        speaker.setVisible(true);
                        speaker.setLocalZOrder(100);
                        speaker.runAction(cc.fadeIn(0.2));
                        var text = new ccui.Text();
                        text.setFontSize(26);
                        text.setColor(cc.color(254, 245, 92));
                        text.setAnchorPoint(cc.p(0, 0));
                        text.enableOutline(cc.color(0, 0, 0), 1);
                        speakerPanel.removeAllChildren();
                        speakerPanel.addChild(text);
                        var showContent = "";
                        switch (gameData.appId) {
                            case APP_ID_SHANXI:
                                showContent = "欢迎来到乐乐麻将!";
                                break;
                            case APP_ID_SHAN3XI:
                            case APP_ID_SHANDONG:
                                showContent = "欢迎来到丫丫麻将!";
                                break;
                            default:
                                showContent = "欢迎来到丫丫麻将!";
                                break;
                        }
                        text.setString(window.inReview ? showContent : content);
                        text.setPositionX(speakerPanel.getContentSize().width);
                        text.setPositionY((speakerPanel.getContentSize().height - text.getContentSize().height) / 2);
                        text.runAction(cc.sequence(
                            cc.delayTime(0.2)
                            , cc.moveTo(duration, -text.getVirtualRendererSize().width, 0)
                            , cc.delayTime(0.3)
                            , cc.callFunc(function () {
                                speaker.runAction(cc.fadeOut(0.2));
                            })
                        ));
                    };
                    func();
                    interval = setInterval(func, totalDuration);
                }
            })());
            this.addCustomListener(4000, function (data) {
                var uid = data['uid'];
                that.throwTurn(uid2position[uid]);
            });
            this.addCustomListener(4001, function (data) {
                var uid = data['uid'];
                var paiId = data['pai_id'];
                var left = data['left'];
                var paiArr = data['pai_arr'];
                var liangPaiArr = data['liang_pai_arr'];
                var isTing = data['isTing'];
                var notChu = data['not_chu'];
                var hasDing = data['hasDing'];
                gameData.hasDing = hasDing;
                leftPaiCnt = left;
                $('timer2.Text_2').setString(left);
                that.mopai(uid2position[uid], paiId, paiArr, liangPaiArr, isTing, notChu, hasDing);


                //听牌提示
                if (that.hupaiTipData ) {
                   if(uid != gameData.uid){
                       that._HuTipLayer.setVisible(true);
                       var hupaiCards = that.hupaiTipData['' + that.chuPaiId];
                       that._HuTipLayer.initHuPaiTishiLayer(hupaiCards);
                   }else {
                       that._HuTipLayer.setVisible(false);
                   }

                }

            });
            this.addCustomListener(4002, function (data) {
                var uid = data['uid'];
                var idx = data['idx'];
                var paiId = data['pai_id'];
                that.chuPaiId = paiId;
                var paiArr = (data['pai_arr'] || []);
                var liangPaiArr = (data['liang_pai_arr'] || []);
                var paiCnt = (data['pai_cnt'] || 0);
                var left = (data['left'] || 0);
                var dingArr = (data['dingArr'] || []);
                var isTing = data['isTing'];
                var row = uid2position[uid];
                that.setLisiByRow(row, data);



                if (mapId == MAP_ID_CHANGSHA)
                    $('timer2.Text_2').setString(left);
                if (!((gameData.mapId == MAP_ID_SHANDONG || gameData.mapId == MAP_ID_LINYI) && isTing)) {
                    for (var i = 0; i < paiCnt; i++)
                        paiArr.push(0);
                }
                if (mapId == MAP_ID_SHANXI_LLFY) {
                    that.chuPai(row, idx, paiId, paiArr, liangPaiArr, isTing, dingArr);
                    if (row == 2) {
                        gameData.dingArr = liangPaiArr;
                    }
                }
                else {
                    that.chuPai(row, idx, paiId, paiArr, liangPaiArr, isTing);
                }

                if (mapId == MAP_ID_WUHAN_KAIKOU || mapId == MAP_ID_WUHAN_KOUKOU) {
                    var isGang = data['gang'] || false;
                    if (isGang) {
                        if (row == 2)
                            network.stop();
                        that.playChiPengGangHuAnim(row, OP_GANG, false, false);
                        playEffect('vgang', position2sex[row]);
                    }
                    var multiple = data['multiple'] || 1;
                    $('info' + row + '.lb_bs').setString('x' + multiple);
                }
            });
            this.addCustomListener(4003, function (data) {
                var uid = data['uid'];
                var fromUid = data['from_uid'];
                var paiId = data['pai_id'];
                var op = data['op'];
                var chi = op[0];
                var peng = op[1];
                var gang = op[2];
                var hu = op[3];
                var ting = op[4];
                if (chi) that.chiPengGangHu(OP_CHI, uid2position[uid], paiId, uid2position[fromUid], data);
                if (peng) that.chiPengGangHu(OP_PENG, uid2position[uid], paiId, uid2position[fromUid], data);
                if (gang) that.chiPengGangHu(OP_GANG, uid2position[uid], paiId, uid2position[fromUid], data);
                if (hu) that.chiPengGangHu(OP_HU, uid2position[uid], paiId, uid2position[fromUid], data);
                if (ting) that.chiPengGangHu(OP_TING, uid2position[uid], paiId, uid2position[fromUid], data);
                if (!chi && !peng && !gang && !hu && !ting)
                    that.chiPengGangHu(OP_PASS, uid2position[uid], paiId, uid2position[fromUid], data);

                if (mapId == MAP_ID_SHANXI_WEINAN) {
                    that.quePaiGray();
                }
            });
            this.addCustomListener(4004, function (data) {
                var uid = data['uid'];
                var paiId = data['pai_id'];
                var op = data['op'];
                var row = uid2position[uid];
                that.showChiPengGangHu(row, paiId, op[0], op[1], op[2], op[3], op[4], data);
            });
            this.addCustomListener(4005, function (data) {
                var uid = data['uid'];
                var op = data['op'];
                var row = uid2position[uid];
                that.showTingLiang(row, op[0], op[1]);
            });
            this.addCustomListener(40044, function (data) {
                var arr = data['arr'];
                for (var i = 0; i < arr.length; i++) {
                    var uid = arr[i]['data']['uid'];
                    var paiId = arr[i]['data']['pai_id'];
                    var op = arr[i]['data']['op'];
                    var row = uid2position[uid];
                    that.showChiPengGangHu(row, paiId, op[0], op[1], op[2], op[3], op[4], data);
                }
            });
            this.addCustomListener(4008, function (data) {
                that.hupaiTipData = null;
                if(that._HuTipLayer){
                    that._HuTipLayer.setVisible(false);
                }
                that.setRoomState(ROOM_STATE_ENDED);
                that.jiesuan(data);
            });
            this.addCustomListener(4009, function (data) {
                that.zongJiesuan(data);
            });
            this.addCustomListener(4010, function (data) {
                var msg = data.msg;
                that.showToast(decodeURIComponent(msg));
            });
            this.addCustomListener(4011, function (data) {
                var zhainiaoLayer = new ZhaniaoLayer(data);
                that.addChild(zhainiaoLayer);
                network.stop();
            });
            this.addCustomListener(4012, function (data) {
                var zhainiaoLayer = new ZhaniaoLayer(data, 2);
                that.addChild(zhainiaoLayer);
            });
            this.addCustomListener(4013, function (data) {
                if (gameData.mapId == MAP_ID_SHANXI_LLFY) {
                    that.chiPengGangHu(OP_TING, uid2position[data['uid']], data['pai_id'], uid2position[data['uid']], data);
                } else {
                    that.showTip("第一步, 请选择一张要打出的牌");
                    liangStep = 1;
                    that.setLisiInfo(data);
                    that.setPaiArrOfRow(2, data['pai_arr'], false, false, data['liang_pai_arr']);
                }


            });
            this.addCustomListener(4014, function (data) {
                var paiArr = data['pai_arr'];
                var paiArrBak = _.clone(data['pai_arr']);
                var gangPaiArr = data['gang_pai_arr'];
                if (!gangPaiArr) {

                }
                else {
                    that.showTip("请选择不想显示的杠牌");
                    $('btn_xuanzewancheng').setVisible(true);
                    TouchUtil.setOnclickListener($('btn_xuanzewancheng'), function () {
                        that.selectHideGangCb.call(that);
                    });
                    for (var i = 0; i < gangPaiArr.length; i++) {
                        while (true) {
                            var j = paiArrBak.indexOf(gangPaiArr[i]);
                            if (j < 0)
                                break;
                            paiArrBak.splice(j, 1);
                        }
                    }
                    hideGangArr = gangPaiArr;
                    hidedGangArr = [];

                    if (gangPaiArr.length == 0)
                        that.selectHideGangCb();
                    else {
                        hideGangStep = 1;
                        that.setLisiInfo(data);
                        that.setPaiArrOfRow(2, paiArr, false, false, gangPaiArr, true);
                    }
                }
            });
            this.addCustomListener(4015, function (data) {
                hideGangChupaiArr = data['ting_pai_arr'];
                hideGangStep = 2;
                that.showTip("请出一张牌");
                var paiArr = that.getPaiArr();
                that.setLisiInfo(data);
                that.setPaiArrOfRow(2, paiArr, false, false, hideGangChupaiArr);
                if ($('btn_xuanzewancheng')) {
                    $('btn_xuanzewancheng').setVisible(false);
                }
            });
            this.addCustomListener(4020, function (data) {
                var uid = data['uid'];
                var isOffline = data['is_offline'];
                that.playerOnloneStatusChange(uid2position[uid], isOffline);
            });

            this.addCustomListener(4200, function (data) {
                var paiArr = data['paiArr'];
                var zhuangUid = data['zhuang_uid'];
                var lianzhuang = data['lianzhuang'];

                gameData.dingArr = data['liang_pai_arr'];
                gameData.zhuangUid = zhuangUid;
                gameData.leftRound = data['left_round'];
                gameData.totalRound = data['total_round'];
                gameData.curRound = data['curRound'];

                var clubTablesLayer = that.getChildByName('clubTablesLayer');
                if (clubTablesLayer) {
                    clubTablesLayer.removeFromParent(true);
                }

                if (mapId != MAP_ID_NANCHANG) {
                    that.initExtraMapData(data);
                }

                that.setRoomState(ROOM_STATE_ONGOING);
                that.clearTable4StartGame(true);
                that.setLisiInfo(data);

                if (mapId == MAP_ID_SHANXI_LLFY) {
                    var otherDing = data['otherDing'];
                    that.fapai(paiArr, otherDing);
                }
                else {
                    that.fapai(paiArr);
                }
                that.setZhuang(uid2position[zhuangUid]);
                if (gameData.appId == APP_ID_ANHUI && gameData.mapId == MAP_ID_ANHUI) {
                    gameData.lianzhuang = data['lianzhuang'];
                    that.setLianZhuang(lianzhuang);
                }
                if (that.afterGameStart)
                    that.afterGameStart();

                that.initDingwei();
            });
            this.addCustomListener(4030, function (data, errCode) {
                if (errCode == -1) {
                    that.showTip('您选择要亮的或要出的牌不正确,请重新选择');
                    $('btn_queding').setVisible(true);
                }
                else {
                    var uid = data['uid'];
                    that.hideTip();
                    var paiArr = data['pai_arr'];
                    var liangPaiArr = data['liang_pai_arr'] || [];

                    var row = uid2position[uid];
                    if (row == 2) {
                        afterLianging = true;
                        that.upPai(2, -1);
                    }
                    else {
                        for (var i = 0; i < paiArr.length; i++)
                            paiArr[i] = 0;
                    }
                    that.setLisiByRow(row, data);
                    for (var i = 0; i < liangPaiArr.length; i++)
                        disabledChuPaiIdMap[liangPaiArr[i]] = true;

                    that.setPaiArrOfRow(row, paiArr, (row != 2), (row != 2), liangPaiArr);

                    if (row == 2) {
                        hasChupai = true;
                        that.hideTip();
                    }
                }
            });
            this.addCustomListener(4031, function (data, errCode) {
                if (errCode == -1) {
                    that.showTip('您选择要出的牌不正确,请重新选择');
                }
                else {
                    var uid = data['uid'];
                    that.hideTip();
                    var paiArr = data['pai_arr'];
                    var hidedGangPaiArr = data['hided_gang_arr'] || [];

                    var row = uid2position[uid];
                    if (row == 2) {
                        afterLianging = true;
                        that.upPai(2, -1);
                    }
                    else {
                        for (var j = 0; j < hidedGangPaiArr.length; j++) {
                            for (var i = 0; i < 4; i++) {
                                that.getGPai(row, i, 0);
                                var g = $('row' + row + '.g' + i);
                                if (g.isVisible() == false) {
                                    that.setGHide(row, i);
                                    g.setVisible(true);
                                    that.recalcPos(row);
                                    break;
                                }
                            }
                        }
                    }

                    if (row == 2) {
                        hasChupai = true;
                        that.hideTip();
                    }

                    if ($('info' + row + '.liang'))
                        $('info' + row + '.liang').setVisible(true);
                    if (gameData.mapId == MAP_ID_SHANXI_LVLIANG || gameData.mapId == MAP_ID_SHANXI_TAIYUAN) {
                        setTimeout(function () {
                            that.playChiPengGangHuAnim(row, OP_TING);
                            playEffect('vliang', position2sex[row]);
                        }, 1200);
                    } else {
                        setTimeout(function () {
                            that.playChiPengGangHuAnim(row, OP_LIANG);
                            playEffect('vliang', position2sex[row]);
                        }, 1200);
                    }
                }
            });
            this.addCustomListener(4041, function (data, errCode) {
            });
            this.addCustomListener(4050, function (data) {
                var arr = data['arr'];
                var haidiUid = data['haidi_uid'] || 0;
                var paiId = data['pai_id'] || 0;
                var allNo = true;
                for (var i = 0; i < arr.length; i++)
                    if (arr[i].val >= 0)
                        allNo = false;
                if (haidiUid) {
                    var haidiLayer = $('haidiLayer', that);
                    if (haidiLayer)
                        haidiLayer.removeFromParent(true);
                    showAlert("玩家【" + gameData.playerMap[haidiUid].nickname + "】获得海底牌: " + PAINAMES[paiId], function () {
                    });
                }
                else if (allNo) {
                    var haidiLayer = $('haidiLayer', that);
                    if (haidiLayer)
                        haidiLayer.removeFromParent(true);
                    showAlert('没有玩家要海底牌');
                }
                else {
                    var haidiLayer = $('haidiLayer', that);
                    if (!haidiLayer) {
                        haidiLayer = new HaidiLayer();
                        haidiLayer.setName('haidiLayer');
                        that.addChild(haidiLayer);
                    }
                    haidiLayer.setArr(arr);
                }
            });
            this.addCustomListener(4052, function (data) {
                network.stop();
                var uid = data['uid'];
                var row = uid2position[uid];
                if (row == 2)
                    afterLianging = true;

                if ($('info' + row + '.liang'))
                    $('info' + row + '.liang').setVisible(true);
                if (row == 2) {
                    that.setLisiInfo(data);
                    that.setPaiArrOfRow(2, that.getPaiArr(), false, false, []);
                    that.getPai(2, 13).setVisible(false);
                }
                var isReconnect = data['is_reconnect'] || false;
                var paiID = data['pai'];
                var paiIDA = data['pai_a'];
                var paiIDB = data['pai_b'];
                var paiStr = PAINAMES[paiIDA] + (paiIDB ? "," + PAINAMES[paiIDB] : "");

                that.showTip("开杠: " + paiStr);

                that.hideChiPengGangHu();

                var throwDiceCb = function () {
                    network.start();
                    var content = "玩家【" + ellipsisStr(_.trim(gameData.playerMap[uid].nickname), 6) + "】开杠" + PAINAMES[paiID] + ", 补牌: " + paiStr + "\n请选择你可以进行的操作";

                    var qgh = data['qgh'];
                    var opA = data['op_a'];
                    var opB = data['op_b'];

                    if (opA[0] + opA[1] + opA[2] + opA[3] <= 0 &&
                        opB[0] + opB[1] + opB[2] + opB[3] <= 0 && !qgh)
                        return;

                    var layer = $('kaigangLayer', that) || that.kaigangLayer;
                    if (!layer) {
                        var scene = ccs.load(res.KaigangLayer_json);
                        layer = scene.node;
                        layer.setName('kaigangLayer');
                        layer.retain();
                        that.kaigangLayer = layer;
                    }
                    that.addChild(layer);

                    $('root.panel.lb_content', layer).setString(content);

                    var paiA = $('root.panel.a0', layer);
                    var paiB = $('root.panel.a1', layer);
                    paiA.setUserData({paiId: paiIDA});
                    paiB.setUserData({paiId: paiIDB});
                    setSpriteFrameByName(paiA, getPaiNameByRowAndId(2, paiIDA, true), that.getPaiPlist());
                    setSpriteFrameByName(paiB, getPaiNameByRowAndId(2, paiIDB, true), that.getPaiPlist());

                    var qghBtn = $('root.panel.btn_qgh', layer);
                    if (qgh) {
                        qghBtn.setVisible(true);
                        TouchUtil.setOnclickListener(qghBtn, function () {
                            network.send(4056, {room_id: gameData.roomId, pai_id: paiID, op: OP_HU});
                            layer.removeFromParent(false);
                        });
                    }
                    else {
                        qghBtn.setVisible(false);
                        TouchUtil.removeListeners(qghBtn);
                    }

                    var enableBtn = function (val, btn) {
                        if (val) {
                            Filter.remove(btn);
                            TouchUtil.setOnclickListener(btn, function () {
                                var op = btn.getName().split('_')[0];
                                var idx = btn.getName().split('_')[1];
                                op = {chi: 1, peng: 2, gang: 50, bu: 3, hu: 4, guo: 0}[op];
                                var pai = btn.getParent().getChildByName('a' + idx);
                                var _paiId = pai.getUserData().paiId;
                                if (op == 1) {
                                    that.showChiLayer(_paiId, function (paiId, oriPaiId) {
                                        network.send(4056, {
                                            room_id: gameData.roomId,
                                            pai_id: paiId,
                                            ori_pai_id: oriPaiId,
                                            op: op
                                        });
                                        layer.removeFromParent(false);
                                    });
                                }
                                else {
                                    network.send(4056, {room_id: gameData.roomId, pai_id: _paiId, op: op});
                                    layer.removeFromParent(false);
                                }
                            });
                        }
                        else {
                            Filter.grayScale(btn);
                            TouchUtil.removeListeners(btn);
                        }
                    };

                    enableBtn(opA[0], $('root.panel.chi_0', layer));
                    enableBtn(opA[1], $('root.panel.peng_0', layer));
                    enableBtn(opA[2], $('root.panel.gang_0', layer));
                    enableBtn(opA[2], $('root.panel.bu_0', layer));
                    enableBtn(opA[3], $('root.panel.hu_0', layer));

                    enableBtn(opB[0], $('root.panel.chi_1', layer));
                    enableBtn(opB[1], $('root.panel.peng_1', layer));
                    enableBtn(opB[2], $('root.panel.gang_1', layer));
                    enableBtn(opB[2], $('root.panel.bu_1', layer));
                    enableBtn(opB[3], $('root.panel.hu_1', layer));

                    TouchUtil.setOnclickListener($('root.panel.guo', layer), function () {
                        if (qgh) {
                            showMessageBox("当前可以抢杠胡, 确定要【过】吗? \n(点击取消可重新选择抢杠胡)", function () {
                                network.send(4056, {room_id: gameData.roomId, pai_id: 0, op: 0});
                                layer.removeFromParent(false);
                            });
                        }
                        else {
                            network.send(4056, {room_id: gameData.roomId, pai_id: 0, op: 0});
                            layer.removeFromParent(false);
                        }
                    });
                };

                if (isReconnect) {
                    network.start();
                    throwDiceCb();
                }
                else
                    that.throwDice("开杠摇骰子", data['dice'], paiIDA, paiIDB, throwDiceCb);
            });
            this.addCustomListener(4053, function (data) {
                that.showTip("请等待他人选择是否小胡");
                disableChupai = true;
                selectingXiaohu = true;
            });
            this.addCustomListener(4054, function (data) {
                that.hideTip();
                disableChupai = false;
                selectingXiaohu = false;
            });
            this.addCustomListener(4055, function (data) {
                that.hideTip();
                disableChupai = false;
                selectingXiaohu = false;

                var uid = data['uid'];
                var row = uid2position[uid];
                if (row != 2) {
                    var paiArr = data['pai_arr'];
                    var paiCnt = data['pai_cnt'];
                    var n = paiCnt - paiArr.length;
                    for (var i = 0; i < n; i++)
                        paiArr.push(0);
                    that.setLisiCount(row, data.lisi_pai_cnt);
                    that.setPaiArrOfRow(row, paiArr, true, false);
                }

                that.playChiPengGangHuAnim(row, OP_HU, false);
                playEffect('vhu', position2sex[row]);
            });
            this.addCustomListener(4058, function (data) {
                var layer = ccs.load(res.KaigangReplayLayer_json).node;
                var content = $('root.lb_content', layer);
                content.setString(decodeURIComponent(data.content));
                that.addChild(layer);
                that.scheduleOnce(function () {
                    layer.removeFromParent(true);
                }, 5);
            });
            this.addCustomListener(4060, function (data) {
                if (data.sel_map) {
                    quePaiList = data.sel_map;
                    if (quePaiList[gameData.uid] < 0 && !that.queMenLayer) {
                        that.queMenLayer = new QueMenLayer(that, data);
                        that.addChild(that.queMenLayer);
                    }
                    that.showQueMen();
                }
            });
            this.addCustomListener(4062, function (data) {
                if (data == null || hasChupai) return;
                afterLianging = false;
                that.hupaiTipData = data.tishi;
                //胡牌提示
                for (var i = 0; i < 14; i++) {
                    (function (i) {
                        var cardSprite = that.getPai(2, i);
                        var userData = cardSprite.getUserData();
                        var paiId = userData.pai;
                        var hucards = null;
                        for (var cards in data.tishi) {
                            if (parseInt(cards) == parseInt(paiId)) {
                                if (mapId != MAP_ID_SHANXI_TUIDAOHU && mapId != MAP_ID_SHANXI_WEINAN && mapId != MAP_ID_SHANXI_ZHUANZHUAN && mapId != MAP_ID_SHANXI_HUASHUI) {
                                    cardSprite.userData.isLiang = true;
                                    Filter.remove(cardSprite);
                                }
                                hucards = data.tishi[cards];
                                break;
                            } else {
                                if (mapId != MAP_ID_SHANXI_TUIDAOHU && mapId != MAP_ID_SHANXI_WEINAN && mapId != MAP_ID_SHANXI_ZHUANZHUAN && mapId != MAP_ID_SHANXI_HUASHUI) {
                                    cardSprite.userData.isLiang = false;
                                    Filter.grayMask(cardSprite);
                                }
                            }
                        }
                        var hucardSprite;
                        if (cardSprite.getChildByName('hucardSprite')) {
                            hucardSprite = cardSprite.getChildByName('hucardSprite');
                            hucardSprite.setPosition(cardSprite.getContentSize().width / 2, cardSprite.getContentSize().height + 20);
                        } else {
                            hucardSprite = new cc.Sprite(cc.textureCache.addImage(res.hupaitip_jiao1));
                            hucardSprite.setPosition(cardSprite.getContentSize().width / 2, cardSprite.getContentSize().height + 20);
                            hucardSprite.setName('hucardSprite');
                            cardSprite.addChild(hucardSprite);
                        }
                        var duration = 2;
                        hucardSprite.setVisible(!!hucards);
                        hucardSprite.runAction(new cc.RepeatForever(cc.sequence(
                            cc.spawn(cc.moveBy(duration, 0, 10), cc.fadeTo(duration, 188)),
                            cc.spawn(cc.moveBy(duration, 0, -10), cc.fadeTo(duration, 255))
                        )));
                        userData.hucards = hucards;
                        TouchUtil.setListeners(hucardSprite, {
                            onTouchBegan: function (node, touch, event) {
                                that.HuCardTip(hucards, cardSprite.getPositionX(), true);
                            }
                            , onTouchMoveOut: function (node, touch, event) {
                                //胡牌提示
                                that.HuCardTip(null, null, false);
                            }
                            , onTouchEnded: function (node, touch, event) {
                                that.HuCardTip(null, null, false);
                            }
                        });
                    })(i);
                }
            });
            this.addCustomListener(4070, function (data) {
                var row = uid2position[data['uid']];
                if (row == 2)
                    afterLianging = true;
                network.stop();
                that.throwDice("开杠摇骰子", data['dice'], data['pai_a'], data['pai_b'], function () {
                    $('info' + uid2position[data['uid']] + '.liang').setVisible(true);
                    network.start();
                });
            });
            this.addCustomListener(4072, function (data) {
                that.initExtraMapData(data);
            });

            this.addCustomListener(4080, function (data) {
                var players_pai = data['players_pai'];
                for (var i = 0; i < players_pai.length; i++) {
                    var uid = players_pai[i]['uid'];
                    var pai_arr = players_pai[i]['pai_arr'];
                    var row = uid2position[uid];
                    if (row != 2) {
                        that.setLisiCount(row, data.lisi_pai_cnt);
                        that.setPaiArrOfRow(row, pai_arr, true, false);
                    }
                    if ($('info' + row + '.liang'))
                        $('info' + row + '.liang').setVisible(true);
                }
            });
            // 用于单独刷新吃碰杠那些放在边上的牌
            this.addCustomListener(4310, function (data) {
                var uid = data['uid'];
                var duiArr = data['dui_arr'];
                if (duiArr) {
                    that.setDuiArr(uid2position[uid], duiArr);
                }
            });
            this.addCustomListener(4066, function (data) {
                // todo data格式recv: {"code":4066,"data":{"scores":[{"uid":11, "score":-100, "total":900}, {"uid":1897, "score":-100, "total":900}, {"uid":5059, "score":100, "total":1100}, {"uid":5060, "score":100, "total":1100}]}}
                if (data && data["scores"]) {
                    var scores = data["scores"];
                    for (var i = 0; i < scores.length; i++) {
                        var row = uid2position[scores[i]["uid"]];
                        that.showChangeScore(row, scores[i]["score"]);
                        $('info' + row + '.lb_score').setString(scores[i]["total"]);
                    }
                }
            });

            network.start();

            this._HuTipLayer = new MaHuTipLayer();
            this._HuTipLayer.setPositionX(this._HuTipLayer.getPositionX() + cc.winSize.width / 2 - 1280 / 2);
            this.addChild(this._HuTipLayer);
            this._HuTipLayer.setVisible(false);

            isCCSLoadFinished = true;

            // playMusic();
            var setting_bgm = settingData.local_bgMusic[gameData.mapId] || 1;
            playMusic("vbg_ma" + setting_bgm);

            that.initTableBack();
            that.showFangzhu();
        },

        initTableBack : function () {
            var  backId = cc.sys.localStorage.getItem('newTableBackKEY') || 0;
            var _bg = res['table_back' + backId];
            $('bg').setTexture(_bg);
        },
        getVersion: function () {
            var versiontxt = window.curVersion ;
            if (isReplay) {
                versiontxt = '';
            }
            var version = new ccui.Text();
            version.setFontSize(15);
            version.setTextColor(cc.color(255, 255, 255));
            version.setPosition(cc.p(cc.winSize.width - 80, 10));
            version.setAnchorPoint(cc.p(1, 0.5));
            version.setString(versiontxt);
            this.addChild(version, 2);
        },
        showFangzhu: function () {
            if (gameData.daikaiPlayer) {
                // $('fangzhuheader').setVisible(true);
                loadImageToSprite(gameData.daikaiPlayer['headimgurl'], $('fangzhuheader.header'));
                // loadImage(gameData.daikaiPlayer['headimgurl'], $('fangzhuheader.header'), false);
                $('fangzhuheader.name').setString(ellipsisStr(gameData.daikaiPlayer['nickname'], 6));
            } else {
                $('fangzhuheader').setVisible(false);
            }
        },
        onExit: function () {
            if (this.chatLayer)
                this.chatLayer.release();
            if (this.settingsLayer)
                this.settingsLayer.release();
            if (this.chiLayer)
                this.chiLayer.release();
            if (this.throwDiceLayer)
                this.throwDiceLayer.release();
            if (this.kaigangLayer)
                this.kaigangLayer.release();
            //同MainLayer里边移除listener,需谨慎,有深意O(∩_∩)O
            // network.removeListeners([
            //     3002, 3003, 3004, 3005, 3008, 3200,
            //     4000, 4001, 4002, 4003, 4004, 4008, 4009, 4020, 4072, 4200
            // ]);

            network.removeListenersForTarget(this);
            cc.Layer.prototype.onExit.call(this);
        },
        onPlayerEnterExit: function () {
            var that = this;
            var players = gameData.players;

            that.refreshPlayersStates();

            for (var i = 0; i < players.length; i++) {
                var player = players[0];
                if (player.uid != gameData.uid) {
                    players.splice(0, 1);
                    players.push(player);
                }
                else
                    break;
            }
            $('timer').setUserData({delta: i});
            uid2position = {};
            for (var i = 0, j = 2; i < players.length; i++, j++) {
                var player = players[i];
                var k = {
                    4: {'0': 0, '1': 3, '2': 2, '3': 1},
                    3: {'0': 1, '1': 3, '2': 2},
                    2: {'0': 2, '1': 0}
                }[playerNum][j % playerNum];

                $('info' + k).setVisible(true);
                // $('info' + k + '.lb_nickname').setString(ellipsisStr(player['nickname'], (k == 0 || k == 2 ? 7 : 5)));
                $('info' + k + '.lb_nickname').setString(ellipsisStr(player['nickname'], 7));
                if (mapId == MAP_ID_SHANXI_LLFY && !isReplay) {
                    $('dingPanel.lb_nickname_' + k).setString(ellipsisStr(player['nickname'], 7));
                }

                var scoreCreated = 0;
                var scoreEnd = 0;
                if (mapId == MAP_ID_SHANXI_DADIANZI) {
                    // if(total_round&&total_round > 8) {
                    scoreCreated = player['score'];
                    // }
                    // else {
                    //     scoreCreated = 100;
                    // }
                    scoreEnd = 0;
                } else if (mapId == MAP_ID_SHANXI_JINGBIAN) {
                    scoreCreated = 0;
                    scoreEnd = 0;
                }
                else if (mapId == MAP_ID_SHANXI_LLFY) {
                    scoreCreated = 0;
                    scoreEnd = 0;
                }
                $('info' + k + '.lb_score').setString(roomState == ROOM_STATE_CREATED ? scoreCreated : player['score'] + scoreEnd);
                if (k != 2 && gameData.uid == gameData.ownerUid && that.getRoomState() != ROOM_STATE_ONGOING) {
                    $('info' + k + '.ti').setVisible(true);
                }
                if (mapId == MAP_ID_WUHAN_KAIKOU || mapId == MAP_ID_WUHAN_KOUKOU)
                    $('info' + k + '.lb_bs').setString('x1');
                if (roomState == ROOM_STATE_CREATED)
                    $('info' + k + '.ok').setVisible(!!player['ready']);
                loadImageToSprite(player['headimgurl'], $('info' + k + '.head'));
                // loadImage(player['headimgurl'], $('info' + k + '.head'), false);
                uid2position[player.uid] = k;
                uid2playerInfo[player.uid] = player;
                position2uid[k] = player.uid;
                position2sex[k] = player.sex;
                position2playerArrIdx[k] = i;
            }

            for (; i < playerNum; i++, j++) {
                var k = {
                    4: {'0': 0, '1': 3, '2': 2, '3': 1},
                    3: {'0': 1, '1': 3, '2': 2},
                    2: {'0': 2,'1': 0}
                }[playerNum][j % playerNum];
                $('info' + k).setVisible(false);
            }

            if (playerNum == 3) {
                $('info0').setVisible(false);
                $('row1').y = 232; // todo 这里暂时写死
                $('row3').y = 0; // todo 这里暂时写死
            }
            if (playerNum == 2) {
                $('info1').setVisible(false);
                $('info3').setVisible(false);
            }
            if (gameData.uid == gameData.ownerUid) {
                if (players.length < playerNum) {
                    $('btn_invite').setVisible(gameData.loginType != 'yk');
                    $('btn_yxks').setVisible(false);
                } else if (that.getRoomState() != ROOM_STATE_ONGOING) {
                    $('btn_invite').setVisible(false);
                    $('btn_yxks').setVisible(true);
                }
            }

            if (gameData.roomClubId && gameData.roomClubId > 0 && this.assistantLayer && that.getRoomState() == ROOM_STATE_CREATED && cc.sys.isObjectValid(this.assistantLayer)) {
                this.assistantLayer.setVisible(players.length != playerNum);
            }
        },
        calcPosConf: function () {
            for (var row = 0; row < 4; row++) {
                var a0 = $('row' + row + '.a0');
                posConf.paiA0PosBak[row] = a0.getPosition();
                posConf.paiA0ScaleBak[row] = [a0.getScaleX(), a0.getScaleY()];
                posConf.headPosBak[row] = $('info' + row).getPosition();

                if (row == 0 || row == 2) {
                    var a0 = $('row' + row + '.a0');
                    var a1 = $('row' + row + '.a1');
                    posConf.paiADistance.push(a1.getPositionX() - a0.getPositionX());
                    var b0 = $('row' + row + '.c0.b0');
                    var b1 = $('row' + row + '.c0.b1');
                    posConf.paiUsedDistance.push(b1.getPositionX() - b0.getPositionX());

                    posConf.downPaiPositionY = a0.getPositionY();
                    posConf.upPaiPositionY = a0.getPositionY() + UPPAI_Y;
                }
                else {
                    var a0 = $('row' + row + '.a0');
                    var a1 = $('row' + row + '.a1');
                    posConf.paiADistance.push(a1.getPositionY() - a0.getPositionY());
                    var b0 = $('row' + row + '.c0.b0');
                    var b1 = $('row' + row + '.c0.b1');
                    posConf.paiUsedDistance.push(b1.getPositionY() - b0.getPositionY());
                }
                var b0 = $('row' + row + '.g0.b0');
                var b2 = $('row' + row + '.g0.b2');
                if (!cc.sys.isObjectValid(b0) || !cc.sys.isObjectValid(b2)) {
                    return;
                }

                if (row == 0) posConf.groupWidth[0] = b0.getPositionX() + b0.getContentSize().width / 2 - (b2.getPositionX() - b2.getContentSize().width / 2);
                if (row == 2) posConf.groupWidth[2] = b2.getPositionX() + b2.getContentSize().width / 2 - (b0.getPositionX() - b0.getContentSize().width / 2);
                if (row == 1) posConf.groupHeight[1] = b2.getPositionY() + b2.getContentSize().height / 2 - (b0.getPositionY() - b0.getContentSize().height / 2);
                if (row == 3) posConf.groupHeight[3] = b0.getPositionY() + b0.getContentSize().height / 2 - (b2.getPositionY() - b2.getContentSize().height / 2);
            }
        },
        setRoomData: function (_data, roomDataType) {
            isReplay = (roomDataType == ROOM_DATA_TYPE.REPLAY);
            isReconnect = (roomDataType == ROOM_DATA_TYPE.RECONNECT);
            data = (isReplay || isReconnect)?_data:null;
        },
        ctor: function (_data, _isReplay) {
            this._super();

            clearVars();

            var that = this;
            forRows = function (cb) {
                if (playerNum == 4 || playerNum == 2)
                    cb.call(that, 0);
                if (playerNum != 2)
                    cb.call(that, 1);
                cb.call(that, 2);
                if (playerNum != 2)
                    cb.call(that, 3);
            };


            this.setRoomData(_data, _isReplay);
            network.stop();

            loadCCSTo(res.MaScene_json, this, "Scene");

            var sceneid = cc.sys.localStorage.getItem('sceneid') || 0;
            this.changeBg(sceneid);
            this.getVersion();
            return true;
        },
        changeBg: function (sceneid) {
            $('bg').setTexture(res['table_back' + sceneid]);
        },

        startSignal: function () {
            var that = this;
            var interval = null;
            var lastDealy = -1;
            var func = function () {
                if (!that || !cc.sys.isObjectValid(that))
                    return clearInterval(interval);
                var delay = network.getLastPingInterval();
                if (delay == lastDealy)
                    return;
                lastDealy = delay;
                if (delay < 200) $('signal').setTexture(signal5);
                else if (delay < 400) $('signal').setTexture(signal4);
                else if (delay < 600) $('signal').setTexture(signal3);
                else if (delay < 800) $('signal').setTexture(signal2);
                else $('signal').setTexture(signal1);
            };
            func();
            interval = setInterval(func, 200);
        },
        startTime: function () {
            var interval = null;
            var flag = true;
            var lbTime = $('lb_time');
            if (isReplay)
                return;
            var battery = $('battery');
            var updTime = function () {
                //显示电池电量
                    var level = DeviceUtil.getBatteryLevel();
                    if (cc.sys.isObjectValid(battery)) {
                        if(level == 100){
                            battery.setTexture(res.battery5);
                        }else if(level > 75){
                            battery.setTexture(res.battery4);
                        }else if(level > 50){
                            battery.setTexture(res.battery3);
                        }else if(level > 25){
                            battery.setTexture(res.battery2);
                        }else if(level > 0){
                            battery.setTexture(res.battery1);
                        }

                    }

                var date = new Date();
                var minutes = (date.getMinutes() < 10 ? '0' : '') + date.getMinutes();
                var hours = (date.getHours() < 10 ? '0' : '') + date.getHours();
                if (cc.sys.isObjectValid(lbTime))
                    lbTime.setString(hours + (flag ? ':' : ' ') + minutes);
                else if (interval)
                    clearInterval(interval);
                flag = !flag;
            };
            updTime();
            interval = setInterval(updTime, 1000);
        },
        isMyTurn: function () {
            return turnRow == 2;
        },
        getTurn: function () {
            for (var i = 0; i < 4; i++) {
                var node = $('timer.' + i);
                if (node.isVisible())
                    return (i + $('timer').getUserData().delta) % 4;
            }
            return -1;
        },
        throwTurnByUid: function (uid) {
            this.throwTurn(uid2position[uid]);
        },
        throwTurn: function (row, isTing, hasDing) {
            turnRow = row;

            $('timer').setVisible(true);
            $('timer2').setVisible(true);
            if (gameData.appId == APP_ID_SHANXI) {
                for (var _i = 0; _i < 4; _i++) {
                    if (_i == row) {
                        var arrow = $('timer.arrow' + _i);
                        arrow.setVisible(true);
                        if (arrow.getNumberOfRunningActions() == 0)
                            arrow.runAction(new cc.RepeatForever(cc.sequence(cc.fadeTo(0.5, 168), cc.fadeTo(0.5, 255))));
                    } else {
                        $('timer.arrow' + _i).setVisible(false);
                    }
                }
            } else {
                for (var _j = 0; _j < 4; _j++){
                    var _arrow = $('timer.arrow' + _j);
                    if (_arrow)
                        _arrow.setVisible(false);
                    $('timer.dir_' + _j).setTextColor(_j == row ? cc.color(0, 0, 0) : cc.color(202, 202, 202));
                }
                var arrow = $('timer.arrow' + row);
                arrow.setTexture(res['t' + row]);
                arrow.setVisible(true);
                if (arrow.getNumberOfRunningActions() == 0)
                    arrow.runAction(new cc.RepeatForever(cc.sequence(cc.fadeTo(0.5, 168), cc.fadeTo(0.5, 255))));
            }

            if (!afterLianging) {
                for (var i = 0; i < 14; i++) {
                    var pai = this.getPai(2, i);
                    var userData = pai.getUserData();
                    if (row == 2/* && !disabledChuPaiIdMap[userData.pai]*/) {
                        if (mapId == MAP_ID_SHANXI_TAIYUAN) {
                            if (typeof lisiPaiCountList[row] === 'number' && i < lisiPaiCountList[row]) {
                                userData.isLiang = false;
                                Filter.heavyGrayMask(pai);
                            } else {
                                userData.isLiang = true;
                                Filter.remove(pai);
                            }
                        }
                        else if (gameData.mapId == MAP_ID_SHANXI_LLFY) {
                            if (userData.isDing) {
                                Filter.grayMask(pai);
                            }
                            else {
                                Filter.remove(pai);
                            }
                        }
                        else {
                            if (isTing) {
                                userData.isLiang = false;
                                Filter.grayMask(pai);
                            }
                            else {
                                userData.isLiang = true;
                                Filter.remove(pai);
                            }
                        }
                    }
                    else {
                        userData.isLiang = false;
                        if (mapId == MAP_ID_SHANXI_TAIYUAN && typeof lisiPaiCountList[2] === 'number' && i < lisiPaiCountList[2]) {
                            Filter.heavyGrayMask(pai);
                        } else {
                            Filter.grayMask(pai);
                        }
                    }
                }
            }

            if (row == 2)
                this.upPai(2, -1);
            this.hideTip();

            if (row == 2 && mapId == MAP_ID_SHANXI_WEINAN) {
                this.quePaiGray();
            }

        },

        isHongzhongLaizi: function () {
            return mapId == MAP_ID_ZHUANZHUAN || (gameData.wanfaDesp && gameData.wanfaDesp.indexOf("红中赖子") >= 0);
        },
        setPai: function (row, idx, val, isLittle, isStand, isVisible) {
            var that =this ;
            var pai = this.getPai(row, idx);

            if (row == 2 && val > 0) {
                var laiziArr = [];
                if (this.isHongzhongLaizi())
                    laiziArr.push(32);
                if (laiziPaiId && laiziArr.indexOf(laiziPaiId) < 0)
                    laiziArr.push(laiziPaiId);

                var lb = pai.getChildByName('lb');
                if (laiziArr.indexOf(val) >= 0) {
                    if (!lb) {
                        lb = new cc.Sprite(res.lb_lai);
                        lb.setName('lb');
                        pai.addChild(lb);
                        lb.setPosition(17.1, 21.5);
                    }
                    lb.setTexture(res.lb_lai);
                    lb.setVisible(true);
                }
                else if (lb)
                    lb.setVisible(false);
            }

            var userData = pai.getUserData();
            var paiName = getPaiNameByRowAndId(row, val, isLittle, val > 0 ? false : (isReplay || roomState == ROOM_STATE_ENDED ? false : isStand));
            userData.pai = val;
            setSpriteFrameByName(pai, paiName, that.getPaiPlist());
            if (!_.isUndefined(isVisible)) {
                if (isVisible)
                    pai.setVisible(true);
                else
                    pai.setVisible(false);
            }
            return pai;
        },
        getPai: function () {
            var cache = {};
            return function (row, id) {
                cache[row] = cache[row] || {};
                if (cache[row][id])
                    return cache[row][id];
                var node = $('row' + row + '.a' + id);
                if (!node) {
                    var a0 = $('row' + row + '.a0');
                    node = duplicateNode(a0, true);
                    if (row == 0 || row == 2) node.setPositionX(posConf.paiADistance[row] * id + a0.getPositionX());
                    if (row == 1 || row == 3) node.setPositionY(posConf.paiADistance[row] * id + a0.getPositionY());
                    node.setName("a" + id);
                    a0.getParent().addChild(node, (row == 1 ? -id : 0));
                }
                var userData = node.getUserData();
                if (!userData)
                    userData = {};
                userData.idx = id;
                node.setUserData(userData);
                // node.userData = userData;
                cache[row][id] = node;
                return node;
            }
        },
        getPaiId: function (row, id) {
            var userData = this.getPai(row, id).getUserData();
            return userData.pai;
        },
        setGChi: function (row, j, paiId, oriPaiId) {
            var a = 0, b = 0, c = 0;
            if (_.isArray(paiId)) {
                a = paiId[0];
                b = paiId[1];
                c = paiId[2];
            }
            else {
                if (paiId - 1 == oriPaiId) {
                    a = paiId;
                    b = paiId - 1;
                    c = paiId + 1;
                }
                if (paiId == oriPaiId) {
                    a = paiId - 1;
                    b = paiId;
                    c = paiId + 1;
                }
                if (paiId + 1 == oriPaiId) {
                    a = paiId - 1;
                    b = paiId + 1;
                    c = paiId;
                }
            }
            this.setGPai(row, j, 0, a);
            this.setGPai(row, j, 1, b);
            this.setGPai(row, j, 2, c);
            $('row' + row + '.g' + j + '.b' + 3).setVisible(false);
            $('row' + row + '.g' + j).setVisible(true);
            this.addNarrow($('row' + row + '.g' + j + '.b' + 0), row, uid2position[fromId]);
        },
        setGPai: function (row, g, idx, val) {
            var pai = this.getGPai(row, g, idx);
            var userData = pai.getUserData();
            var paiName = getPaiNameByRowAndId(row, val, true);
            userData.pai = val;
            setSpriteFrameByName(pai, paiName, this.getPaiPlist());
        },
        setGPeng: function (row, j, paiId, fromRow, peng_pai_Cnt) {
            // peng_pai_Cnt = 1;
            var arr = [paiId, paiId, paiId];
            if (mapId == MAP_ID_SHANXI_LLFY) {
                for (var k = 0; k < peng_pai_Cnt; k++) {
                    var lb = this.getGPai(row, j, k).getChildByName('lb');
                    if (!lb) {
                        lb = new cc.Sprite(res.lb_liang);
                        lb.setTexture(res.lb_liang);
                        lb.setName('lb');
                        this.getGPai(row, j, k).addChild(lb);
                        lb.setPosition(17.1, 21.5);
                    }
                    else
                        lb.setTexture(res.lb_liang);
                    lb.setVisible(true);
                }
                for (k = peng_pai_Cnt; k < 3; k++) {
                    var lb = this.getGPai(row, j, k).getChildByName('lb');
                    if (lb) {
                        lb.setVisible(false);
                    }
                }
            }
            if (mapId == MAP_ID_PINGXIANG258)
                arr[posConf.gPaiPos[row][fromRow]] = 0;
            for (var k = 0; k < arr.length; k++)
                this.setGPai(row, j, k, arr[k]);
            $('row' + row + '.g' + j + '.b' + 3).setVisible(false);
            $('row' + row + '.g' + j).setVisible(true);
            this.addNarrow($('row' + row + '.g' + j + '.b' + 0), row,fromRow);
        },
        setGHide: function (row, j) {
            for (var k = 0; k < 3; k++)
                this.setGPai(row, j, k, 0);
            $('row' + row + '.g' + j + '.b' + 3).setVisible(false);
            $('row' + row + '.g' + j).setVisible(true);
        },
        setGGang: function (row, j, upPaiId, downPaiId, fromRow, gang_pai_Cnt) {
            var arr = [downPaiId, downPaiId, downPaiId];
            if (mapId == MAP_ID_PINGXIANG258) {
                if (downPaiId) {
                    if (row != fromRow)
                        arr[posConf.gPaiPos[row][fromRow]] = 0;
                }
                else
                    upPaiId = 0;
                arr[3] = upPaiId;
                if (arr[3] != arr[1])
                    arr[3] = arr[1];
                this.setGPai(row, j, 3, arr[3]);
            } else if (mapId == MAP_ID_SHANXI_LLFY) {
                //hjz gang 做判断
                if (downPaiId == 0) {
                    upPaiId = 0;
                }
                this.setGPai(row, j, 3, upPaiId);
                if (gang_pai_Cnt == 3) {

                }
                else if (gang_pai_Cnt == 2) {
                    for (var k = 2; k < 4; k++) {
                        if (k != 1) {
                            var lb = this.getGPai(row, j, k).getChildByName('lb');
                            if (!lb) {
                                lb = new cc.Sprite(res.lb_liang);
                                lb.setTexture(res.lb_liang);
                                lb.setName('lb');
                                this.getGPai(row, j, k).addChild(lb);
                                lb.setPosition(17.1, 21.5);
                            }
                            else
                                lb.setTexture(res.lb_liang);
                            lb.setVisible(true);
                        }
                    }
                    for (var k = 0; k < 2; k++) {
                        var lb = this.getGPai(row, j, k).getChildByName('lb');
                        if (lb) {
                            lb.setVisible(false);
                        }
                    }
                }
                else if (gang_pai_Cnt == 1) {
                    var lb = this.getGPai(row, j, 3).getChildByName('lb');
                    if (!lb) {
                        lb = new cc.Sprite(res.lb_liang);
                        lb.setTexture(res.lb_liang);
                        lb.setName('lb');
                        this.getGPai(row, j, 3).addChild(lb);
                        lb.setPosition(17.1, 21.5);
                    }
                    else
                        lb.setTexture(res.lb_liang);
                    lb.setVisible(true);
                    for (var k = 0; k < 3; k++) {
                        var lb = this.getGPai(row, j, k).getChildByName('lb');
                        if (lb) {
                            lb.setVisible(false);
                        }
                    }
                }
                else {
                    for (var k = 0; k < 4; k++) {
                        var lb = this.getGPai(row, j, k).getChildByName('lb');
                        if (lb) {
                            lb.setVisible(false);
                        }
                    }
                }
            }
            else {
                this.setGPai(row, j, 3, upPaiId);
            }
            $('row' + row + '.g' + j + '.b' + 3).setVisible(true);
            for (var k = 0; k < 3; k++)
                this.setGPai(row, j, k, arr[k]);
            $('row' + row + '.g' + j).setVisible(true);
            this.addNarrow($('row' + row + '.g' + j + '.b' + 0), row,fromRow);
        },
        setGJiaGang: function (row, paiId, fromRow) {
            for (var j = 0; j < 4; j++)
                if ($('row' + row + '.g' + j).isVisible() &&
                    this.getGPaiId(row, j, 0) == paiId &&
                    this.getGPaiId(row, j, 0) == this.getGPaiId(row, j, 1) &&
                    this.getGPaiId(row, j, 1) == this.getGPaiId(row, j, 2)) {
                    this.setGPai(row, j, 3, paiId);
                    this.getGPai(row, j, 3).setVisible(true);
                    if (mapId == MAP_ID_PINGXIANG258) {
                        if (row != fromRow) {
                            var t = posConf.gPaiPos[row][fromRow];
                            t = (t == 1 ? 3 : t);
                            this.setGPai(row, t, 3, 0);
                        }
                    }
                    break;
                }
            this.addNarrow($('row' + row + '.g' + j + '.b' + 0), row,fromRow);
        },
        getGPai: function () {
            var cache = {};
            return function (row, g, id) {
                cache[row] = cache[row] || {};
                cache[row][g] = cache[row][g] || {};
                if (cache[row][g][id])
                    return cache[row][g][id];
                var group0 = $('row' + row + '.g' + 0);
                var groupNode = $('row' + row + '.g' + g);
                if (!groupNode) {
                    groupNode = duplicateNode(group0,true);
                    if (row == 0 || row == 2) groupNode.setPositionX(group0.getPositionX() + posConf.groupWidth[row] * g + posConf.paiADistance[row] * g);
                    if (row == 1 || row == 3) groupNode.setPositionY(group0.getPositionY() + posConf.groupHeight[row] * g + posConf.paiADistance[row] * g);
                    groupNode.setName("g" + g);
                    groupNode.setVisible(false);
                    group0.getParent().addChild(groupNode);
                }
                var node = $('row' + row + '.g' + g + '.b' + id);
                cache[row][g][id] = node;
                return node;
            }
        },
        getGPaiId: function (row, g, id) {
            var userData = $('row' + row + '.g' + g + '.b' + id).getUserData();
            return userData.pai;
        },
        checkPaiAmount: function () {
            if (cc.sys.isNative)
                return;
            var amount = 0;
            var row = 2;
            for (var j = 0; j < 4; j++) {
                var g = $('row' + row + '.g' + j);
                if (!g || !$('row' + row + '.g' + j).isVisible())
                    break;
                else if (mapId == MAP_ID_SHANXI_LLFY) {
                    for (var k = 0; k < 3; k++) {
                        if ($('row' + row + '.g' + j + '.b' + k).isVisible()) {
                            amount += 1;
                        }
                    }
                }
                else {
                    amount += 3;
                }
            }
            for (var j = 0; j < 14; j++)
                if (this.getPai(row, j).isVisible())
                    amount += 1;
            if (amount < 13 || amount > 14)
                alert("你的牌数量可能不对: " + amount + ", 数一下");
        },
        recalcPos: function (row) {
            var g0 = $('row' + row + '.g' + 0);
            var g0b0pos = getPositionRelativeToParent(this.getGPai(row, 0, 0), 2);
            var groupWidthllfy = 0;
            var groupHeightllfy = 0;
            for (var i = 1; i < 4; i++) {
                var g = $('row' + row + '.g' + i);
                if (g && g.isVisible()) {
                    if (row == 0 || row == 2)
                        g.setPositionX(g0.getPositionX() + (posConf.groupWidth[row] * i + posConf.groupDistance[row] * i) * (row == 2 ? 1 : -1));
                    if (row == 1 || row == 3)
                        g.setPositionY(g0.getPositionY() + (posConf.groupHeight[row] * i + posConf.groupDistance[row] * i) * (row == 1 ? 1 : -1));
                }
                else
                    break;
            }
            var k = i;
            var m = (k > 1 || g0.isVisible() ? 1 : 0);

            var a0 = this.getPai(row, 0);
            var to = 0;
            var p = (row == 1 || row == 2 ? 1 : -1);
            if (row == 0 || row == 2) to = !m ? posConf.paiA0PosBak[row].x : g0b0pos.x + (posConf.groupWidth[row] * k + posConf.groupDistance[row] * k + posConf.groupToFirstPaiDistance[row]) * p;
            if (row == 1 || row == 3) to = !m ? posConf.paiA0PosBak[row].y : g0b0pos.y + (posConf.groupHeight[row] * k + posConf.groupDistance[row] * k + posConf.groupToFirstPaiDistance[row]) * p;
            // }
            if (row == 0 || row == 2) a0.setPositionX(to);
            if (row == 1 || row == 3) a0.setPositionY(to);
            for (var i = 1; i < 13; i++) {
                var a = this.getPai(row, i);
                var userData = a.getUserData();
                if (row == 0) a.setPosition(a0.getPositionX() + i * posConf.paiADistance[row] + (a.getUserData().pai > 0) * i * posConf.paiALiangDistance[row], a0.getPositionY());
                if (row == 2) {
                    a.setPositionX(a0.getPositionX() + i * posConf.paiADistance[row] + (a.getUserData().pai > 0) * i * posConf.paiALiangDistance[row]);
                    if (!userData.isUpping && !userData.isDowning)
                        a.setPositionY(userData.isUp ? posConf.upPaiPositionY : posConf.downPaiPositionY);
                }
                if (row == 1 || row == 3) a.setPosition(a0.getPositionX(), a0.getPositionY() + i * posConf.paiADistance[row] + (a.getUserData().pai > 0) * i * posConf.paiALiangDistance[row]);
            }

            // recalc pai 13 position
            for (var i = 12; i >= 0; i--) {
                var pai = this.getPai(row, i);
                if (pai && pai.isVisible()) {
                    var userData = pai.getUserData();
                    var p = (row == 1 || row == 2 ? 1 : -1);
                    if (row == 0) this.getPai(row, 13).setPosition(pai.getPositionX() + posConf.paiADistance[row] + posConf.paiMopaiDistance[row] * p, a0.getPositionY());
                    if (row == 2) {
                        this.getPai(row, 13).setPositionX(pai.getPositionX() + posConf.paiADistance[row] + posConf.paiMopaiDistance[row] * p);
                        if (!userData.isUpping && !userData.isDowning)
                            a.setPositionY(userData.isUp ? posConf.upPaiPositionY : posConf.downPaiPositionY);
                    }
                    if (row == 1 || row == 3) this.getPai(row, 13).setPosition(a0.getPositionX(), pai.getPositionY() + posConf.paiADistance[row] + posConf.paiMopaiDistance[row] * p);
                    break;
                }
            }

            posConf.paiPos[row] = [];
            for (var i = 0; i < 14; i++) {
                posConf.paiPos[row].push(this.getPai(row, i).getPosition());
            }
            for (var i = 14; i < 18; i++) {
                var pai = $('row' + row + '.a' + i);
                if (pai)
                    pai.setVisible(false);
            }
        },
        hidePai: function (row, id) {
            this.getPai(row, id).setVisible(false);
        },
        getPaiArr: function () {
            var arr = [];
            for (var j = 0; j < 14; j++) {
                var pai = this.getPai(2, j);
                var userData = pai.getUserData();
                if (userData.pai > 0)
                    arr.push(userData.pai);
            }
            return arr;
        },
        addUsedPai: function () {
            var that =this;
            var cache = {};
            return function (row, val) {
                cache[row] = cache[row] || {};
                for (var j = 0; j < 60; j++) {
                    var pai = cache[row][j] || $('row' + row + '.c0.b' + j);
                    if (!pai) {
                        var k = (j >= 0 && j < 10 ? 0 : parseInt(j / 10) * 10);
                        var b = $('row' + row + '.c0.b' + k);
                        if (!b) {
                            var b0 = $('row' + row + '.c0.b0');
                            var b10 = $('row' + row + '.c0.b10');
                            b = duplicateNode(b0,true);
                            b.setName('b' + k);
                            if (row == 0) b.setPositionY(parseInt(j / 10) * (b10.getPositionY() - b0.getPositionY()) + b0.getPositionY());
                            if (row == 1) b.setPositionX(parseInt(j / 10) * (b10.getPositionX() - b0.getPositionX()) + b0.getPositionX());
                            if (row == 2) b.setPositionY(parseInt(j / 10) * (b10.getPositionY() - b0.getPositionY()) + b0.getPositionY());
                            if (row == 3) b.setPositionX(parseInt(j / 10) * (b10.getPositionX() - b0.getPositionX()) + b0.getPositionX());
                            b0.getParent().addChild(b);
                            if (row == 2)
                                b.setLocalZOrder(k * -1);
                            pai = b;
                            pai.setVisible(false);
                        }
                        if (!pai) {
                            pai = duplicateNode(b,true);
                            if (row == 0 || row == 2) pai.setPositionX(posConf.paiUsedDistance[row] * (j - k) + b.getPositionX());
                            if (row == 1 || row == 3) pai.setPositionY(posConf.paiUsedDistance[row] * (j - k) + b.getPositionY());
                            pai.setName("b" + j);
                            pai.setVisible(false);
                            b.getParent().addChild(pai, j * posConf.paiUsedZOrder[row][k]);
                            cache[row][j] = pai;
                        }
                    }

                    pai.setUserData({idx: j, pai: val});

                    if (!pai.isVisible()) {
                        var paiName = getPaiNameByRowAndId(row, val, true);
                        //userData.pai = val;
                        setSpriteFrameByName(pai, paiName,that.getPaiPlist());
                        pai.setVisible(true);
                        return pai;
                    }
                }
            }
        },
        removeOneTopUsedPai: function (row, paiId) {
            for (var j = 50; j >= 0; j--) {
                var pai = $('row' + row + '.c0.b' + j);
                if (pai && pai.isVisible()) {
                    var userData = pai.getUserData();
                    if (!_.isUndefined(paiId) && userData.pai != paiId)
                        return;
                    userData.pai = 0;
                    pai.setVisible(false);
                    break;
                }
            }
            this.hideArrow();
        },
        getRoomState: function () {
            return roomState;
        },
        setRoomState: function (state) {
            var arr = decodeURIComponent(gameData.wanfaDesp).split(',');
            if (arr.length >= 1 && mapId != MAP_ID_SHANXI_DADIANZI)
                arr = arr.slice(1);
            if (mapId == MAP_ID_SHANXI_TKSJ) {
                for (var i = 0; i < arr.length; i++) {
                    if (arr[i] == "停口上架") {
                        arr = arr.slice(1);
                        break;
                    }
                }
            }
            var wanfaStr = arr.join(" ");
            if (state == ROOM_STATE_CREATED) {

                try {
                    if (gameData.roomClubId && !isReplay) {
                        this.assistantLayer = this.getChildByName('assistantLayer');
                        if (!this.assistantLayer) {
                            this.assistantLayer = new ClubAssistantLayer(playerNum);
                            this.assistantLayer.setName('assistantLayer');
                            this.addChild(this.assistantLayer);
                        }
                    }
                } catch (e) {
                    console.log('error in this.assistantLayer e :' + e.toString());
                }

                $('btn_control_btns').setVisible(true);
                this.hideControlBtns();
                $('signal').setVisible(false);
                $('setting').setVisible(false);
                $('chat').setVisible(false);
                $('timer').setVisible(false);
                if ($('timer2'))
                    $('timer2').setVisible(false);
                for(var row=0; row < 4; row++){
                    $('info' +row+'.people_bg').setVisible((false));
                    $('info' +row+'.lb_nickname').setVisible((true));
                    $('info' +row+'.lb_score').setVisible((false));
                }
                $('btn_invite').setVisible(gameData.loginType != 'yk' && gameData.uid == gameData.ownerUid);

                if(gameData.is_club){
                    $("btn_fanhui").setVisible(true);
                }else{
                    $("btn_fanhui").setVisible(gameData.uid != gameData.ownerUid);
                }

                $('btn_jiesan').setVisible(gameData.uid == gameData.ownerUid);
                $('btn_zhunbei').setVisible(false);
                $('btn_mic').setVisible(false);
                if (mapId == MAP_ID_SHANXI_LLFY && !isReplay) {
                    $('btn_liang').setVisible(false);
                    for (var i = 0; i < 4; i++) {
                        $('dingPanel.mingpai_' + i).setVisible(false);
                    }
                }

                $('lb_roomid').setString(gameData.roomId);
                $('timer2.Text_9').setString(gameData.roomId);
                $('timer2.Text_7').setString(mapId2Name(mapId));
                $('lb_wanfa').setString(wanfaStr);
                $('lb_roomid').setVisible(true);
                // $('lb_version').setString(window.inReview ? '' : (''+ window.curVersion || ''));
                // getVersion('majiang', $('lb_version'));
                $('lb_version').setVisible(true);
                $('row0').setVisible(false);
                $('row1').setVisible(false);
                $('row2').setVisible(false);
                $('row3').setVisible(false);
                if ($('btn_queding')) $('btn_queding').setVisible(false);
                if ($('btn_quxiao')) $('btn_quxiao').setVisible(false);
                if ($('btn_xuanzewancheng')) $('btn_xuanzewancheng').setVisible(false);
                for (var i = 0; i < 4; i++) {
                    $('info' + i).setPosition($('info_n' + i).getPosition());
                }
                this.hideArrow();
                disabledChuPaiIdMap = {};

                lianging = false;
                afterLianging = false;
            }
            if (state == ROOM_STATE_ONGOING) {
                if (this.assistantLayer && cc.sys.isObjectValid(this.assistantLayer)) {
                    this.assistantLayer.removeFromParent(true);
                    this.assistantLayer = null;
                }

                var setting = $('setting');
                if (!setting || !cc.sys.isObjectValid(setting))
                    return network.disconnect();
                $('signal').setVisible(!isReplay);
                $('signal').setVisible(!window.inReview);
                $('btn_control_btns').setVisible(!isReplay);
                $('timer').setVisible(true);
                $('timer2').setVisible(true);
                $('chat').setVisible(true)
                $('lb_roomid').setVisible(false);
                $('lb_roomid_').setVisible(false);
                $('btn_invite').setVisible(false);
                $('btn_fanhui').setVisible(false);
                $('btn_jiesan').setVisible(false);
                $('btn_zhunbei').setVisible(false);
                $('btn_yxks').setVisible(false);
                for(var row=0; row<4; row++){
                    $('info' +row+'.people_bg').setVisible(true);
                    $('info' +row+'.lb_nickname').setVisible(false);
                    $('info' +row+'.lb_score').setVisible(true);
                }

                $('btn_mic').setVisible(!window.inReview);

                $('lb_roomid').setString(gameData.roomId);
                $('timer2.Text_9').setString(gameData.roomId);
                $('timer2.Text_7').setString(mapId2Name(mapId));
                // getVersion('majiang_gs', $('lb_version'));
                // $('lb_version').setString(window.inReview ? '' :  window.curVersion || '');
                // $('lb_version').setVisible(true);
                $('lb_wanfa').setString(wanfaStr);
                $('row0.c0').setVisible(true);
                if ($('btn_queding')) {
                    $('btn_queding').setVisible(false);
                }
                if ($('btn_quxiao')) {
                    $('btn_quxiao').setVisible(false);
                }
                if ($('btn_xuanzewancheng')) {
                    $('btn_xuanzewancheng').setVisible(false);
                }
                $('row0').setVisible(true);
                $('row1').setVisible(true);
                $('row2').setVisible(true);
                $('row3').setVisible(true);

                if (mapId == MAP_ID_SHANXI_DADIANZI || (mapId == MAP_ID_SHANXI_HUASHUI && gameData.totalRound == 100))
                    $('timer2.Text_4').setString('当前');
                else
                    $('timer2.Text_4').setString('还剩');

                if(gameData.mapId == MAP_ID_SHANXI_HUASHUI && gameData.totalRound == 100){
                    $('timer2.Text_5').setString((gameData.totalRound - gameData.leftRound));
                }else{
                    $('timer2.Text_5').setString(gameData.leftRound);
                }

                $('row0.mid').removeAllChildren();
                $('row1.mid').removeAllChildren();
                $('row2.mid').removeAllChildren();
                $('row3.mid').removeAllChildren();

                for (var i = 0; i < 4; i++) {
                    if (roomState == ROOM_STATE_CREATED)
                        $('info' + i).runAction(cc.moveTo(0.5, posConf.headPosBak[i]));
                    else
                        $('info' + i).setPosition(posConf.headPosBak[i]);

                    $('info' + i + '.ok').setVisible(false);
                    $('info' + i + '.ti').setVisible(false);
                }

                if (isReplay) {
                    this.hideControlBtns();
                    $('setting').setVisible(false);
                    $('chat').setVisible(false);
                }
            }
            if (state == ROOM_STATE_ENDED) {
                if (this.assistantLayer && cc.sys.isObjectValid(this.assistantLayer)) {
                    this.assistantLayer.removeFromParent(true);
                    this.assistantLayer = null;
                }
                $('timer2.Text_9').setString(gameData.roomId);
                $('timer2.Text_7').setString(mapId2Name(mapId));

                $('signal').setVisible(!window.inReview);
                $('timer').setVisible(false);
                $('timer2').setVisible(false);
                $('lb_roomid').setVisible(true);
                $('lb_roomid_').setVisible(true);
                $('chat').setVisible(true)
                if ($('btn_queding')) $('btn_queding').setVisible(false);
                if ($('btn_quxiao')) $('btn_xuanzewancheng').setVisible(false);
                if ($('btn_xuanzewancheng')) $('btn_xuanzewancheng').setVisible(false);
                this.hideChiPengGangHu();
                this.hideArrow();
                disabledChuPaiIdMap = {};

                lianging = false;
                afterLianging = false;

                hideGangStep = 0;
                hideGangArr = [];
                hidedGangArr = [];
                hideGangChupaiArr = [];
            }
            roomState = state;
        },
        setDuiArr: function (row, duiArr) {
            for (var j = 0; j < 4; j++) {
                var g = $('row' + row + '.g' + j);
                if (g && g.isVisible())
                    g.setVisible(false);
            }
            for (var j = 0; j < duiArr.length; j++) {
                var dui = duiArr[j];
                var fromRow = uid2position[dui['from_uid']];
                if (dui.type == 1) this.setGChi(row, j, dui['pai_arr']);
                if (dui.type == 2) {
                    if (mapId == MAP_ID_SHANXI_LLFY) {
                        var peng_pai_cnt = 0;
                        for (var _i = 0; _i < dui['pai_arr'].length; _i++) {
                            if (dui['pai_arr'][_i] != 0) peng_pai_cnt++;
                        }
                        if (peng_pai_cnt == 3) {
                            peng_pai_cnt = 0;
                        }
                        this.setGPeng(row, j, dui['pai_arr'][0], fromRow, peng_pai_cnt);
                    }
                    else {
                        this.setGPeng(row, j, dui['pai_arr'][0], fromRow);
                    }
                }
                if (dui.type == 3) {
                    if (mapId == MAP_ID_SHANXI_LLFY) {
                        var peng_pai_cnt = 0;
                        for (var _i = 0; _i < dui['pai_arr'].length; _i++) {
                            if (dui['pai_arr'][_i] != 0)
                                peng_pai_cnt++;
                        }
                        this.setGGang(row, j, dui['pai_arr'][0], dui['pai_arr'][0], fromRow, peng_pai_cnt);
                    }
                    else {
                        this.setGPeng(row, j, dui['pai_arr'][0], fromRow);
                        this.setGGang(row, j, dui['pai_arr'][0], dui['pai_arr'][0], fromRow);
                    }
                }
                if (dui.type == 4) this.setGGang(row, j, dui['pai_arr'][0], 0);
                if (dui.type == 5) this.setGHide(row, j);
            }
        },
        clearTable4StartGame: function (isInitPai, isReconnect, reconnectData) {
            var that = this;
            quePaiList = {};
            this.showQueMen();
            turnRow = null;
            this.onPlayerEnterExit();
            if (isInitPai) {
                forRows(function (i) {
                    var a0 = $('row' + i + '.a0');
                    if (a0.getPosition().x != posConf.paiA0PosBak[i].x || a0.getPosition().y != posConf.paiA0PosBak[i].y) {
                        a0.setPosition(posConf.paiA0PosBak[i]);
                        for (var j = 1; j < 13; j++) {
                            var node = $('row' + i + '.a' + j);
                            if (i == 0 || i == 2) node.setPositionX(posConf.paiADistance[i] * j + a0.getPositionX());
                            if (i == 1 || i == 3) node.setPositionY(posConf.paiADistance[i] * j + a0.getPositionY());
                        }
                    }
                });
                for (var i = 0; i < 4; i++) {
                    for (var j = 0; j < 4; j++) {
                        this.getPai(i, j).setVisible(false);
                    }
                    for (var k = 0; k < 4; k++) {
                        var g = $('row' + i + '.g' + k);
                        if (g)
                            g.setVisible(false);
                    }
                    for (var k = 0; k < 4; k++) {
                        this.getGPai(i, 0, k).setUserData({idx: k});
                    }
                    $('row' + i + '.c0.b0').setVisible(false);
                    $('row' + i + '.c0.b1').setVisible(false);
                    $('row' + i + '.c0.b10').setVisible(false);
                }
                forRows(function (i) {
                    for (var j = 0; j < 14; j++) {
                        this.getPai(i, j).setOpacity(0);
                    }
                    var c0 = $('row' + i + '.c0');
                    if (i == 0)
                        c0.setLocalZOrder(50);
                    for (var j = 0; j < 60; j++) {
                        var t = $('b' + j, c0);
                        if (t) {
                            t.setUserData({idx: j});
                            t.setVisible(false);
                        }
                    }
                });
                for (var i = 0; i < 14; i++) {
                    var pai = this.getPai(2, i);
                    var userData = pai.getUserData();
                    userData.hucards = null;
                }
            }

            $('cpghg').setVisible(false);
            $('timer').setVisible(false);

            if (isReconnect) {
                hasChupai = reconnectData['has_chu'];
                that.setZhuang(uid2position[gameData.zhuangUid]);
                if (gameData.appId == APP_ID_ANHUI && gameData.mapId == MAP_ID_ANHUI) {
                    that.setLianZhuang(gameData.lianzhuang);
                }

                that.initDingwei();
                leftPaiCnt = reconnectData['left_pai_num'];
                $('timer2.Text_2').setString(leftPaiCnt);
                var playerPaiArr = reconnectData['player_pai'];
                for (var i = 0; i < playerPaiArr.length; i++) {
                    var playerPai = playerPaiArr[i];
                    var isOffline = !!playerPai['is_offline'];
                    if (isOffline)
                        this.playerOnloneStatusChange(uid2position[playerPai.uid], isOffline);
                    var row = uid2position[playerPai.uid];
                    if (row != 2
                        && roomState == ROOM_STATE_ONGOING
                        && playerPai['pai_arr'].length == 0) {
                        for (var j = 0; j < playerPai['cur_pai_num']; j++)
                            playerPai['pai_arr'].push(0);
                    }
                    if (row == 2 && playerPaiArr[i]['is_ting'])
                        if (gameData.appId == APP_ID_ANHUI || gameData.appId == APP_ID_SHAN3XI) {
                        } else {
                            afterLianging = true;
                        }
                    //hjz
                    if ($('info' + row + '.liang'))
                        $('info' + row + '.liang').setVisible(playerPaiArr[i]['is_ting']);
                    if (mapId == MAP_ID_SHANXI_LLFY && this.getRoomState() == ROOM_STATE_ENDED)
                        that.setPaiArrOfRow(row, playerPai['pai_arr'], (row != 2), (row != 2));
                    else
                        that.setPaiArrOfRow(row, playerPai['pai_arr'], (row != 2), (row != 2), playerPai['liang_pai_arr']);
                    var liangPaiArr = playerPai['liang_pai_arr'] || [];
                    for (var j = 0; j < liangPaiArr.length; j++)
                        disabledChuPaiIdMap[liangPaiArr[j]] = true;

                    var usedPaiArr = playerPaiArr[i]['used_pai_arr'];
                    for (var j = 0; j < usedPaiArr.length; j++)
                        this.addUsedPai(row, usedPaiArr[j]);
                    var duiArr = playerPaiArr[i]['dui_arr'];
                    this.setDuiArr(row, duiArr);

                    if (mapId == MAP_ID_WUHAN_KAIKOU || mapId == MAP_ID_WUHAN_KOUKOU)
                        $('info' + row + '.lb_bs').setString('x' + playerPai['multiple']);
                    if (mapId == MAP_ID_SHANXI_LVLIANG) {
                        if (row == 2 && playerPai.is_ting) {
                            for (var m = 0; m < playerPai['pai_arr'].length; m++) {
                                var pai = this.getPai(2, m);
                                Filter.grayMask(pai);
                                pai.getUserData().isLiang = false;
                            }
                        }
                    }
                    //hjz zhunbei
                    if (mapId == MAP_ID_SHANXI_LLFY) {
                        if (row == 2) {
                            var dingArr = playerPai['liang_pai_arr'];
                            gameData.dingArr = dingArr;
                            var tempPaiArr = _.clone(playerPai['pai_arr']);
                            tempPaiArr = this.sortPaiArrForLisi(tempPaiArr, dingArr);
                            for (var k = 0; k < playerPai['pai_arr'].length; k++) {
                                this.getPai(2, k).userData.isDing = false;
                                this.getPai(2, k).userData.isLiang = true;
                                var lb = this.getPai(2, k).getChildByName('lb');
                                if (lb) {
                                    lb.setVisible(false);
                                }
                            }
                            for (var j = 0; j < dingArr.length; j++) {
                                var _index = tempPaiArr.indexOf(dingArr[j]);
                                if (_index != -1) {
                                    this.getPai(2, _index).userData.isDing = true;
                                    var lb = this.getPai(2, _index).getChildByName('lb');
                                    if (!lb) {
                                        lb = new cc.Sprite(res.lb_liang);
                                        lb.setTexture(res.lb_liang);
                                        lb.setName('lb');
                                        this.getPai(2, _index).addChild(lb);
                                        lb.setPosition(17.1, 21.5);
                                    }
                                    else
                                        lb.setTexture(res.lb_liang);
                                    lb.setVisible(true);
                                    if (playerPai['hasDing']) {
                                        this.getPai(2, _index).userData.isLiang = false;
                                    }

                                    delete tempPaiArr[_index];
                                }

                            }
                        }
                        var dingArr = playerPai['dingArray'];
                        dingArr.sort(compareTwoNumbers);
                        if (!isReplay) {
                            for (var k = 0; k < 8; k++) {
                                $("dingPanel.mingpai_" + row + ".b" + k).setVisible(false);
                            }

                            for (var k = 0; k < dingArr.length; k++) {
                                var paiName = getPaiNameByRowAndId(row, dingArr[k], false, false);
                                $("dingPanel.mingpai_" + row + ".b" + k).setVisible(true);
                                setSpriteFrameByName($("dingPanel.mingpai_" + row + ".b" + k), paiName, that.getPaiPlist());
                            }
                        }
                        if (row == 2 && playerPai.is_ting) {
                            for (var m = 0; m < playerPai['pai_arr'].length; m++) {
                                var pai = this.getPai(2, m);
                                Filter.grayMask(pai);
                                pai.getUserData().isLiang = false;
                            }
                        }
                    }
                }

                forRows(function (i) {
                    this.recalcPos(i);
                });

                if (roomState == ROOM_STATE_ONGOING) {
                    if (reconnectData['turn_uid'] != -1) {
                        this.throwTurn(uid2position[reconnectData['turn_uid']]);
                    }
                    this.enableChuPai();
                }
                else if (roomState == ROOM_STATE_ENDED) {
                    for (var i = 0; i < playerPaiArr.length; i++) {
                        var playerPai = playerPaiArr[i];
                        var isReady = !!playerPai['is_ready'];
                        if (isReady)
                            that.setReady(playerPai['uid']);
                    }
                }
            }
            else if (isInitPai) {
                forRows(function (i) {
                    this.recalcPos(i);
                    if (mapId == MAP_ID_SHANXI_TAIYUAN) {
                        for (var k = 0; k < 4; k++) {
                            Filter.heavyGrayMask(that.getPai(i, k));
                        }
                    }
                });
            }
        },
        sendChuPai: function (row, pai) {
            network.stop([3008, 4002, 4020,3007,4990]);
            if (mapId == MAP_ID_SHANXI_LLFY) {
                if (!gameData.hasDing && pai.userData.isDing)
                    network.send(4002, {
                        room_id: gameData.roomId,
                        pai_id: pai.userData.pai,
                        idx: pai.userData.idx,
                        isDingPai: true
                    });
                else
                    network.send(4002, {
                        room_id: gameData.roomId,
                        pai_id: pai.userData.pai,
                        idx: pai.userData.idx,
                        isDingPai: false
                    });
            }
            else
                network.send(4002, {room_id: gameData.roomId, pai_id: pai.userData.pai, idx: pai.userData.idx});
            hasChupai = true;
            this.setHuTipVisible(false);
        },
        sendTingChuPai: function (row, idx, paiId) {
            network.stop([3008, 4002, 4020]);
            network.send(4015, {room_id: gameData.roomId, chu_pai_id: paiId, chu_pai_idx: idx});
            hasChupai = true;
            this.setHuTipVisible(false);
        },

        addNarrow: function (pai, row, fromrow) {
            if (pai && cc.sys.isObjectValid(pai) && pai.isVisible()) {
                var from = posConf.gPaiPos[row][fromrow];
                if (from == undefined) {
                    from = 3;
                }
                var texture = res['narrow_' + from];
                if (texture) {
                    var narrow = pai.getChildByName('narrow');
                    if (narrow) {
                        narrow.setTexture(texture);
                    } else {
                        narrow = new cc.Sprite(texture);
                        pai.addChild(narrow);
                        narrow.setName('narrow');
                    }

                    // narrow.runAction(cc.rotateTo(0, posConf.huFromAngle[row]));
                    narrow.setRotation(posConf.huFromAngle[row]);
                    narrow.setPosition(posConf.fromPos[row]);
                }
            }
        },

        chuPai: function (row, idx, paiId, paiArr, liangPaiArr, isTing, _dingArr) {
            var that = this;

            if (liangStep == 2) {
                liangStep = 0;
                that.downPai(row, idx);
                return;
            }

            if (row != 2 && !isReplay)
                idx = 13;

            playEffect('vCardOut');

            var duration = 0.25;
            var delayDuration = 0.3;

            var pai = this.getPai(row, idx);
            var midNode = $('row' + row + '.mid');
            var moveToMidPos = midNode.getPosition();
            var usedPai = this.addUsedPai(row, paiId);
            usedPai.setOpacity(0);
            var moveToFinalPos = cc.p(
                usedPai.getPositionX() + usedPai.getParent().getPositionX(),
                usedPai.getPositionY() + usedPai.getParent().getPositionY()
            );

            that.playerOnloneStatusChange(row, false);
            network.stop([3008, 4001, 4002, 4020]);
            var positionBak = pai.getPosition();
            var scaleBak = pai.getScaleX();

            var userData = pai.getUserData();
            moveToMidPos.y += UPPAI_Y;
            pai.setVisible(false);

            var _paiBg = new cc.Sprite(res.mjbg);
            _paiBg.setScale(CHUPAI_MID_NODE_SCALE_MAP[row] / midNode.getParent().getScaleX());
            _paiBg.setName('paiBg');
            _paiBg.setOpacity(188);
            midNode.addChild(_paiBg);

            var _pai = new cc.Sprite();
            _pai.setName('pai');
            midNode.addChild(_pai);

            var paiName = getPaiNameByRowAndId(2, paiId, false, false);
            setSpriteFrameByName(_pai, paiName, that.getPaiPlist());
            _pai.setScale(CHUPAI_MID_NODE_SCALE_MAP[row] / midNode.getParent().getScaleX());
            _pai.setVisible(true);
            _paiBg.setVisible(true);
            var fangda = cc.sys.localStorage.getItem('fangda') == 1;
            if (fangda) {
                _pai.runAction(cc.sequence(cc.delayTime(delayDuration), cc.callFunc(function () {
                    _pai.removeFromParent(true);
                    _paiBg.removeFromParent(true);
                })));
            } else {
                _pai.removeFromParent(true);
                _paiBg.removeFromParent(true);
            }
            that.setPaiArrOfRow(row, paiArr, (row != 2), (row != 2), []);

            if (row == 2) {
                pai.setPosition(posConf.paiPos[2][idx]);
                pai.setScale(posConf.paiA0ScaleBak[row][0], posConf.paiA0ScaleBak[row][1]);
                that.upPai(row, -1);
                that.recalcPos(row);
                that.showArrow(usedPai, row);

                that.checkPaiAmount();
            }
            else {
                pai.setPosition(positionBak);
                pai.setScale(scaleBak);

                that.setPaiArrOfRow(row, paiArr, (row != 2), (row != 2), liangPaiArr);
                that.recalcPos(row);
                that.showArrow(usedPai, row);
            }

            usedPai.runAction(cc.fadeIn(duration));
            network.start();

            playEffect('vp' + paiId, position2sex[row]);
        },
        checkPaiPos: function (row) {
            for (var j = 0; j < 14; j++) {
                var pai = this.getPai(row, j);
                var userData = pai.getUserData();
                if (!userData.isUpping && !userData.isDowning)
                    pai.setPosition(posConf.paiPos[2][j].x, (userData.isUp ? posConf.upPaiPositionY : posConf.downPaiPositionY));
            }
        },
        upPai: function (row, idx) {
            var that = this;
            this.checkPaiPos(row);
            if (liangStep == 2 && idx >= 0) {
                var pai = that.getPai(row, idx);
                var userData = pai.getUserData();
                if (userData.isUp && !userData.isDowning) {
                    userData.isDowning = true;
                    pai.runAction(cc.sequence(
                        cc.moveTo(UPDOWNPAI_ANIM_DURATION, pai.getPositionX(), (!userData.isUp ? posConf.upPaiPositionY : posConf.downPaiPositionY))
                        , cc.callFunc(function () {
                            userData.isUp = false;
                            userData.isDowning = false;
                        })
                    ));
                }
                else if (!userData.isUpping) {
                    userData.isUpping = true;
                    pai.runAction(cc.sequence(
                        cc.moveTo(UPDOWNPAI_ANIM_DURATION, pai.getPositionX(), (!userData.isUp ? posConf.upPaiPositionY : posConf.downPaiPositionY))
                        , cc.callFunc(function () {
                            userData.isUp = true;
                            userData.isUpping = false;
                        })
                    ));
                }
                return;
            }
            for (var i = 0; i < 14; i++) {
                (function (j) {
                    var pai = that.getPai(row, j);
                    var userData = pai.getUserData();
                    if (idx == j && userData.isUp) {
                        that.sendChuPai(row, pai);
                        userData.isUp = false;
                    }
                    else if (idx == j && !userData.isUp && !userData.isUpping) {
                        userData.isUpping = true;
                        pai.runAction(cc.sequence(
                            cc.moveTo(UPDOWNPAI_ANIM_DURATION, pai.getPositionX(), (!userData.isUp ? posConf.upPaiPositionY : posConf.downPaiPositionY))
                            , cc.callFunc(function () {
                                userData.isUp = true;
                                userData.isUpping = false;
                            })
                        ));
                    }
                    else if (idx != j && userData.isUp && !userData.isDowning) {
                        userData.isDowning = true;
                        pai.runAction(cc.sequence(
                            cc.moveTo(UPDOWNPAI_ANIM_DURATION, pai.getPositionX(), (!userData.isUp ? posConf.upPaiPositionY : posConf.downPaiPositionY))
                            , cc.callFunc(function () {
                                userData.isUp = false;
                                userData.isDowning = false;
                            })
                        ));
                    }
                })(i);
            }
        },
        downPai: function (row, idx) {
            var that = this;
            this.checkPaiPos(row);
            var pai = that.getPai(row, idx);
            var userData = pai.getUserData();
            if (!userData.isUp || userData.isDowning)
                return;
            userData.isDowning = true;
            pai.runAction(cc.sequence(
                cc.moveTo(UPDOWNPAI_ANIM_DURATION, pai.getPositionX(), (!userData.isUp ? posConf.upPaiPositionY : posConf.downPaiPositionY))
                , cc.callFunc(function () {
                    userData.isUp = false;
                    userData.isDowning = false;
                })
            ));
        },
        enableChuPai: function () {
            if (enableChupaiCnt > 0)
                return;
            enableChupaiCnt++;

            var that = this;

            var curPaiIdx = -1;
            var beganTime, beganPosition;
            var isUp, paiIdx, paiId;
            var positionBak;
            var safeY;
            var toNodeDelta = {};
            var chupaiListener = cc.EventListener.create({
                event: cc.EventListener.TOUCH_ONE_BY_ONE,
                swallowTouches: true,
                onTouchBegan: function (touch, event) {
                    if (hasChupai || !that.isMyTurn() || afterLianging || disableChupai || roomState != ROOM_STATE_ONGOING)
                        return;

                    var pai, _curPaiIdx = -1;
                    for (var i = 0; i < 14; i++) {
                        pai = that.getPai(2, i);
                        var userData = pai.getUserData();
                        if (!pai.isVisible())
                            continue;
                        if (TouchUtil.isTouchMe(pai, touch, event, null)) {
                            _curPaiIdx = i;
                            break;
                        }
                    }

                    if ((curPaiIdx >= 0 && _curPaiIdx != curPaiIdx) || !(pai.getNumberOfRunningActions() == 0 || !pai.getUserData().isUpping && !pai.getUserData().isDowning) || !pai.getUserData().isLiang)
                        return false;

                    curPaiIdx = _curPaiIdx;

                    if (curPaiIdx >= 0) {
                        var userData = pai.getUserData();

                        for (var k = 0; k < 14; k++)
                            if (that.getPai(2, k).getNumberOfRunningActions() > 0)
                                return false;

                        that.playEffect('vCardClick');

                        beganTime = getCurrentTimeMills();

                        // back up
                        userData.positionBak = pai.getPosition();
                        userData.scaleBak = pai.getScaleX();

                        isUp = userData.isUp;
                        if (!isUp || liangStep == 2)
                            that.upPai(2, userData.idx);
                        if (userData.hucards)
                            that.HuCardTip(userData.hucards, pai.getPositionX(), true);
                        paiIdx = userData.idx;
                        paiId = userData.pai;
                        positionBak = _.clone(posConf.paiPos[2][curPaiIdx]);
                        positionBak.y = (true || isUp ? posConf.upPaiPositionY : posConf.downPaiPositionY);
                        beganPosition = touch.getLocation();
                        safeY = pai.getBoundingBox().height;
                        var a = touch.getLocation();
                        var b = pai.convertToNodeSpace(touch.getLocation());
                        toNodeDelta.x = a.x - b.x;
                        toNodeDelta.y = a.y - b.y;
                    }

                    return curPaiIdx >= 0;
                },
                onTouchMoved: function (touch, event) {
                    if (curPaiIdx < 0)
                        return;

                    var pai = that.getPai(2, curPaiIdx);
                    var userData = pai.getUserData();

                    if (!userData.isLiang)
                        return;

                    if (beganPosition) {
                        var p = touch.getLocation();
                        p.x -= toNodeDelta.x;
                        p.y -= toNodeDelta.y;
                        if (p.y < safeY) {
                            if (pai.getNumberOfRunningActions() <= 0)
                                pai.setPosition(positionBak);
                            return;
                        }
                        p = pai.convertToNodeSpace(touch.getLocation());
                        p.x += pai.getPositionX() - pai.getBoundingBox().width / 2;
                        p.y += pai.getPositionY() - pai.getBoundingBox().height / 2;
                        pai.setPosition(p);
                    }
                },
                onTouchEnded: function (touch, event) {
                    that.HuCardTip(null, null, false);
                    if (curPaiIdx < 0)
                        return;

                    var pai = that.getPai(2, curPaiIdx);

                    if (beganPosition) {
                        var p = touch.getLocation();
                        p.x -= toNodeDelta.x;
                        p.y -= toNodeDelta.y;
                        var now = getCurrentTimeMills();
                        var isSend = (p.y < safeY && isUp && Math.abs(now - beganTime) < 168) || (p.y > safeY);
                        if (isSend) {
                            if (liangStep == 0 && hideGangStep == 0) {
                                that.sendChuPai(2, pai);
                            } else if (hideGangStep === 2) {
                                that.sendTingChuPai(2, paiIdx, paiId);
                            }
                        }
                        else {
                            that.checkPaiPos(2);
                            setTimeout(function () {
                                curPaiIdx = -1;
                                beganPosition = null;
                            }, parseInt(UPDOWNPAI_ANIM_DURATION * 100));
                        }
                    }
                    curPaiIdx = -1;
                    beganPosition = null;
                    return false;
                }
            });

            return cc.eventManager.addListener(chupaiListener, $("row2"));
        },
        getPaiPlist: function () {
            return majiang_yz.res.pai0plist;
        },
        countDown: function () {
            var that = this;
            var timer = null;
            return function (seconds) {
                if (isReplay) {
                    if (gameData.appId == APP_ID_SHANXI) {
                        $('timer.sec1').setString('');
                        $('timer.sec2').setString('');
                    } else {
                        $('timer.sec').setString('');
                    }
                    return;
                }
                if (timer) {
                    clearInterval(timer);
                    timer = null;
                }
                if (gameData.appId == APP_ID_SHANXI) {
                    $('timer.sec1').setString(seconds % 10);
                    $('timer.sec2').setString(Math.floor(seconds / 10));
                } else {
                    $('timer.sec').setString(seconds);
                    $('timer.sec').setVisible(true);
                }
                timer = setInterval(function () {
                    var sec;
                    if (gameData.appId == APP_ID_SHANXI) {
                        sec = $('timer.sec1');
                    } else {
                        sec = $('timer.sec');
                    }
                    if (!cc.sys.isObjectValid(sec)) {
                        if (timer)
                            clearInterval(timer);
                        return;
                    }
                    if (!sec) {
                        clearInterval(timer);
                        timer = null;
                        return;
                    }
                    if (seconds > 0) {
                        --seconds;
                        if (gameData.appId == APP_ID_SHANXI) {
                            sec.setString(seconds % 10);
                            $('timer.sec2').setString(Math.floor(seconds / 10));
                        } else {
                            sec.setString(seconds < 10 ? "0" + seconds : seconds);
                        }
                    }
                    if (seconds == 0) {
                        if (turnRow == 2 && !hasChupai)
                            DeviceUtil.vibrate();
                        clearInterval(timer);
                    }
                }, 1000);
            }
        },
        closePai: function (paiId) {

        },
        setPaiArrOfRow: function (row, paiArr, isLittle, isStand, liangPaiArr, liangAll, huPaiId) {
            liangPaiArr = liangPaiArr || [];

            if (row == 2)
                _.pull(paiArr, 0);
            if (mapId == MAP_ID_SHANXI_TAIYUAN) {
                if (lisiPaiArr[row] && lisiPaiArr[row].length > 0) {
                    paiArr = this.sortPaiArrForLisi(paiArr, row);
                } else {
                    paiArr.sort(compareTwoNumbers);
                }
            }
            else if (mapId == MAP_ID_SHANXI_LLFY) {
                if ((row == 2 && liangPaiArr || isReplay))
                    paiArr = this.sortPaiArrForLisi(paiArr, liangPaiArr);
                else {
                    paiArr.sort(compareTwoNumbers);
                }
            }
            else {
                paiArr.sort(compareTwoNumbers);
            }
            if (this.isHongzhongLaizi()) {
                var oldSize = paiArr.length;
                _.pull(paiArr, 32);
                if (paiArr.length != oldSize) {
                    var t = paiArr.length;
                    for (var j = 0; j < oldSize - t; j++)
                        paiArr.splice(0, 0, 32);
                }
            }
            if (huPaiId) {
                var idx = paiArr.indexOf(huPaiId);
                if (idx >= 0) {
                    paiArr.splice(idx, 1);
                    paiArr.push(huPaiId);
                }
            }
            var lastPai = null;
            for (var j = 0; j < paiArr.length; j++) {
                lastPai = this.setPai(row, j, paiArr[j], isLittle, isStand, true);
                lastPai.setOpacity(255);
                if (mapId == MAP_ID_SHANXI_TAIYUAN) {
                    if (typeof lisiPaiCountList[row] === 'number') {
                        if (j < lisiPaiCountList[row]) {
                            Filter.heavyGrayMask(lastPai);
                            lastPai.getUserData().isLiang = false;
                        } else {
                            if (row != 2) {
                                Filter.remove(lastPai);
                                lastPai.getUserData().isLiang = true;
                            }
                        }
                    }
                }
            }
            for (; j < 14; j++)
                this.setPai(row, j, 0, isLittle, isStand, false);
            if (row == 2) {
                this.liangAllPai();
            }
            liangPaiArr.sort(compareTwoNumbers);
            if (typeof liangPaiArr !== 'undefined' && liangPaiArr.length > 0) {
                var cond = (row == 2);
                if (mapId == MAP_ID_CHANGSHA)
                    cond = (cond && afterLianging);
                else if (mapId == MAP_ID_SHANDONG
                    || mapId == MAP_ID_LINYI
                    || mapId == MAP_ID_ANHUI
                    || mapId == MAP_ID_HEBEI
                    || mapId == MAP_ID_SHANXI_LVLIANG
                    || mapId == MAP_ID_SHANXI_TAIYUAN)
                    cond = true;
                else
                    cond = (cond && hideGangStep > 0);
                if (cond) {
                    if (mapId == MAP_ID_CHANGSHA) {
                        var pai = this.getPai(2, 13);
                        Filter.grayMask(pai);
                        pai.getUserData().isLiang = false;
                    }
                    for (var i = 0; i < paiArr.length; i++) {
                        var pai = this.getPai(2, i);
                        pai.getUserData().isLiang = false;
                        if (mapId == MAP_ID_SHANXI_TAIYUAN && typeof lisiPaiCountList[row] === 'number' && i < lisiPaiCountList[row]) {
                            Filter.heavyGrayMask(pai);
                        } else {
                            Filter.grayMask(pai);
                        }
                    }
                    for (var j = 0; j < liangPaiArr.length; j++) {
                        for (var i = 0; i < paiArr.length; i++) {
                            var pai = this.getPai(2, i);
                            var userData = pai.getUserData();
                            if (userData.pai == liangPaiArr[j] && !userData.isLiang) {
                                userData.isLiang = true;
                                Filter.remove(pai);
                                if (!liangAll)
                                    break;
                            }
                        }
                    }
                }
                else {
                    if (row == 1 && !isReplay)
                        for (var j = 0; j < liangPaiArr.length; j++)
                            this.setPai(row, j, liangPaiArr[j], isLittle, isStand, true);
                    if (row == 3 && !isReplay)
                        for (var j = 0; j < liangPaiArr.length; j++)
                            this.setPai(row, j, liangPaiArr[j], isLittle, isStand, true);
                    if (row == 0 && !isReplay)
                        for (var j = 0; j < liangPaiArr.length; j++)
                            this.setPai(row, j, liangPaiArr[j], isLittle, isStand, true);
                }
            }
            else if (false && disabledChuPaiIdMap) {
                for (var i = 0; i < paiArr.length; i++) {
                    if (disabledChuPaiIdMap[paiArr[i]]) {
                        var pai = this.getPai(2, i);
                        Filter.grayMask(pai);
                        pai.getUserData().isLiang = false;
                    }
                }
            }
            if (huPaiId) {
                lastPai.setOpacity(168);
                this.recalcPos(row);
            }
        },
        mopai: function (row, paiId, paiArr, liangPaiArr, isTing, notChu, hasDing) {
            if (row == 2) {
                hasChupai = !!((gameData.mapId == MAP_ID_SHANDONG || gameData.mapId == MAP_ID_LINYI || gameData.mapId == MAP_ID_SHANXI3 || gameData.mapId == MAP_ID_HEBEI || gameData.mapId == MAP_ID_SHANXI_DADIANZI || gameData.mapId == MAP_ID_SHANXI_LLFY) && isTing);
                if (mapId == MAP_ID_SHANXI_LLFY) {

                }
                else {
                    this.setPaiArrOfRow(2, paiArr, false, false, liangPaiArr);
                }
            }
            this.setPai(row, 13, (row == 2 || isReplay ? paiId : 0), (row != 2), (row != 2), true)
                .runAction(cc.fadeIn(MOPAI_ANIM_DURATION));
            if (notChu) {
                return;
            }

            this.throwTurn(row, isTing);

            if (row == 2)
                this.recalcPos(2);
            if (mapId == MAP_ID_SHANXI_LLFY && row == 2 || isReplay) {
                for (var i = 0; i < paiArr.length; i++) {
                    if (this.getPai(row, i).userData.isDing) {
                        if (!isTing) {
                            if (hasDing) {
                                this.getPai(row, i).userData.isLiang = false;
                            }
                            else {
                                this.getPai(row, i).userData.isLiang = true;
                            }
                            Filter.grayMask(this.getPai(row, i));
                        }
                        var lb = this.getPai(row, i).getChildByName('lb');
                        if (!lb) {
                            lb = new cc.Sprite(res.lb_liang);
                            lb.setTexture(res.lb_liang);
                            lb.setName('lb');
                            this.getPai(row, i).addChild(lb);
                            lb.setPosition(17.1, 21.5);
                        }
                        else
                            lb.setTexture(res.lb_liang);
                        lb.setVisible(true);
                    }
                    else {
                        if (!isTing) {
                            this.getPai(row, i).userData.isLiang = true;
                            Filter.remove(this.getPai(row, i));
                        }
                        var lb = this.getPai(row, i).getChildByName('lb');
                        if (lb) {
                            lb.setVisible(false);
                        }
                    }
                }
                this.getPai(2, 13).userData.isDing = false;
                this.getPai(2, 13).userData.isLiang = true;
            }
            // }

            this.countDown(12);
        },
        showChiPengGangHu: function (row, paiId, chi, peng, gang, hu, ting, data) {
            var that = this;

            if (!isReplay) {
                var _opPaiIdArr = {};

                var children = $('cpghg').getChildren();
                for (var i = 0; i < children.length; i++) {
                    children[i].setVisible(false);
                }

                if (mapId == MAP_ID_CHANGSHA) {
                    if (!gang) {
                        var nextGang = data['next_gang'];
                        if (nextGang) {
                            _opPaiIdArr[OP_GANG] = nextGang;
                            _opPaiIdArr[OP_TING] = nextGang;
                            gang = 1;
                        }
                    }

                    ting = gang;
                }

                if (mapId == MAP_ID_PINGXIANG258 && gang) {
                    ting = gang;
                }

                var sum = (chi || 0) + (peng || 0) + (gang || 0) + (hu || 0) + (ting || 0) + 1;

                var resGap = 20;
                if (that.isIconMove) {
                    resGap = 0;
                }
                else {
                    resGap = 20;
                    that.isIconMove = true;
                }

                $('cpghg.chi').setVisible(false);
                $('cpghg.chi').setPositionX($('cpghg.chi').getPositionX() - resGap);
                $('cpghg.peng').setVisible(false);
                $('cpghg.peng').setPositionX($('cpghg.peng').getPositionX() - resGap);
                $('cpghg.gang').setVisible(false);
                $('cpghg.gang').setPositionX($('cpghg.gang').getPositionX() - resGap);
                $('cpghg.hu').setVisible(false);
                $('cpghg.hu').setPositionX($('cpghg.hu').getPositionX() - resGap);
                $('cpghg.ting').setVisible(false);
                $('cpghg.ting').setPositionX($('cpghg.ting').getPositionX() - resGap);
                var nodeArr = [], opArr = [];
                if (chi) {
                    nodeArr.push($('cpghg.chi'));
                    opArr.push(OP_CHI);
                }
                if (peng) {
                    nodeArr.push($('cpghg.peng'));
                    opArr.push(OP_PENG);
                }
                if (gang) {
                    nodeArr.push($('cpghg.gang'));
                    opArr.push(OP_GANG);
                }
                if (hu) {
                    nodeArr.push($('cpghg.hu'));
                    opArr.push(OP_HU);
                }
                if (ting) {
                    nodeArr.push($('cpghg.ting'));
                    opArr.push(OP_TING);
                }
                nodeArr.push($('cpghg.guo'));

                for (var i = 0; i < nodeArr.length; i++) {
                    if (i < nodeArr.length - 1) {
                        var node = $('a', nodeArr[i]);
                        if (!node) {
                            var node = duplicateNode($('row2.c0.b0'),true);
                            node.setName('a');
                            node.setPosition($('n', nodeArr[i]).getPosition());
                            node.setVisible(true);
                            nodeArr[i].addChild(node);
                        }
                        var _paiId = _opPaiIdArr[opArr[i]] || paiId;
                        var paiName = getPaiNameByRowAndId(2, _paiId, true);
                        setSpriteFrameByName(node, paiName, that.getPaiPlist());
                        node.setUserData({paiId: _paiId});
                    }

                    if (posConf.cpghgPositionX[sum]) {
                        if (typeof posConf.cpghgPositionX[sum][i] === 'number')
                            nodeArr[i].setPositionX(posConf.cpghgPositionX[sum][i]);
                        else
                            nodeArr[i].setPosition(posConf.cpghgPositionX[sum][i]);
                    }
                    nodeArr[i].setVisible(true);
                }

                if ((mapId == MAP_ID_SHANXI3 || mapId == MAP_ID_SHANXI_DADIANZI) && ting) {
                    $('cpghg.ting').setTexture(res.ting);
                    node.setVisible(false);
                }

                chi && TouchUtil.setOnclickListener($('cpghg.chi'), function () {
                    var _paiId = $('cpghg.chi.a').getUserData().paiId || paiId;
                    if (mapId == MAP_ID_PINGXIANG258) {
                        showMessageBox("确定要【吃】吗?", function () {
                            that.showChiLayer(_paiId, function (paiId, oriPaiId) {
                                that.sendChiPengGang(OP_CHI, 2, paiId, oriPaiId);
                            });
                        }, function () {
                        });
                    }
                    else {
                        that.showChiLayer(_paiId, function (paiId, oriPaiId) {
                            that.sendChiPengGang(OP_CHI, 2, paiId, oriPaiId);
                        });
                    }
                });

                peng && TouchUtil.setOnclickListener($('cpghg.peng'), function () {
                    var _paiId = $('cpghg.peng.a').getUserData().paiId || paiId;
                    if (mapId == MAP_ID_SHANXI_LLFY) {
                        var _func = function (lisi_pai_cnt) {
                            that.sendPengForLLFY(OP_PENG, 2, _paiId, lisi_pai_cnt);
                        };
                        var _inLisiCnt = 0;
                        var _totalCnt = 0;
                        for (var _i = 0; _i < gameData.dingArr.length; _i++) {
                            if (gameData.dingArr[_i] == _paiId) {
                                _inLisiCnt++;
                            }
                        }

                        if (_inLisiCnt == 3)
                            _func(2);
                        else
                            _func(_inLisiCnt);
                    } else {
                        that.sendChiPengGang(OP_PENG, 2, _paiId);
                        that.hideChiPengGangHu();
                    }
                });

                ting && TouchUtil.setOnclickListener($('cpghg.ting'), function () {
                    var _paiId = $('cpghg.ting.a').getUserData().paiId || paiId;
                    if (mapId == MAP_ID_CHANGSHA || mapId == MAP_ID_PINGXIANG258) {
                        var isKaigang = data['kaigang'];
                        if (!isKaigang) {
                            showMessageBox("确定要【打骰子开杠】吗?", function () {
                                that.sendChiPengGang(OP_KAIGANG, 2, _paiId);
                                that.hideChiPengGangHu();
                            }, function () {
                            });
                        }
                        else {
                            that.sendChiPengGang(OP_KAIGANG, 2, _paiId);
                            that.hideChiPengGangHu();
                        }
                    }
                    else if (mapId == MAP_ID_WUHAN_KAIKOU || mapId == MAP_ID_WUHAN_KOUKOU) {

                    }
                    else if (mapId == MAP_ID_KWX_XF || mapId == MAP_ID_SHANXI_LVLIANG || mapId == MAP_ID_SHANXI_TAIYUAN) {
                        that.hideChiPengGangHu();
                        network.send(4013, {
                            room_id: gameData.roomId,
                            uid: gameData.uid
                        });
                    } else if (mapId == MAP_ID_ANHUI || mapId == MAP_ID_SHANXI3 || mapId == MAP_ID_HEBEI
                        || mapId == MAP_ID_SHANDONG || gameData.mapId == MAP_ID_LINYI || gameData.mapId == MAP_ID_SHANXI_DADIANZI
                        || gameData.mapId == MAP_ID_SHANXI_WEINAN || gameData.mapId == MAP_ID_YONGNIAN || gameData.mapId == MAP_ID_SHANXI_TKSJ) {
                        that.sendChiPengGang(OP_TING, 2, _paiId);
                    }
                    else if (mapId == MAP_ID_SHANXI_LLFY) {
                        that.hideChiPengGangHu();
                        network.send(4013, {room_id: gameData.roomId, op: OP_TING, pai_id: _paiId});
                    }
                });

                hu && TouchUtil.setOnclickListener($('cpghg.hu'), function () {
                    var _paiId = $('cpghg.hu.a').getUserData().paiId || paiId;
                    do {
                        if (mapId == MAP_ID_CHANGSHA) {
                            if (selectingXiaohu && leftPaiCnt == 55) {
                                that.sendChiPengGang(OP_XIAOHU, 2, _paiId);
                                that.hideChiPengGangHu();
                                break;
                            }
                        }

                        that.sendChiPengGang(OP_HU, 2, _paiId);
                        that.hideChiPengGangHu();
                    } while (false);
                });

                gang && TouchUtil.setOnclickListener($('cpghg.gang'), function () {
                    var _paiId = $('cpghg.gang.a').getUserData().paiId || paiId;
                    if (mapId == MAP_ID_CHANGSHA) {
                        var isKaigang = data['kaigang'];
                        if (!isKaigang) {
                            showMessageBox("确定要【开杠】吗?", function () {
                                that.sendChiPengGang(OP_KAIGANG, 2, _paiId);
                                that.hideChiPengGangHu();
                            }, function () {
                            });
                        }
                        else {
                            that.sendChiPengGang(OP_KAIGANG, 2, _paiId);
                            that.hideChiPengGangHu();
                        }
                    }
                    else if (mapId == MAP_ID_SHANXI_LLFY) {
                        var _func = function (lisi_pai_cnt) {
                            that.sendGangForLLFY(OP_GANG, 2, _paiId, lisi_pai_cnt);
                        };
                        var _inLisiCnt = 0;
                        var _totalCnt = 0;
                        for (var _i = 0; _i < gameData.dingArr.length; _i++) {
                            if (gameData.dingArr[_i] == _paiId) {
                                _inLisiCnt++;
                            }
                        }

                        _func(_inLisiCnt);
                    }
                    else {
                        that.sendChiPengGang(OP_GANG, 2, _paiId);
                        that.hideChiPengGangHu();
                    }
                });

                TouchUtil.setOnclickListener($('cpghg.guo'), function () {
                    if (hu && that.isMyTurn()) {
                        showMessageBox("您当前可以自摸, 确定要点【过】吗?", function () {
                            that.sendChiPengGang(OP_PASS, 2, paiId);
                            that.hideChiPengGangHu();
                        }, function () {
                            return;
                        });
                    }
                    else {
                        that.sendChiPengGang(OP_PASS, 2, paiId);
                        that.hideChiPengGangHu();
                    }
                });

                $('cpghg').setVisible(true);

                this.countDown(12);
            } else {
                var cpg = $('cpghRep' + row);
                for (var i = 0; i < 6; i++) {
                    if (!cpg.getChildByName("sp1_" + i)) {
                        var sp = new cc.Sprite(res['sp1_' + i]);
                        sp.setPosition(cpg.getChildByName("sp_" + i).getPosition());
                        sp.setName("sp1_" + i);
                        cpg.addChild(sp);
                        sp.setVisible(false);
                    }
                }
                $('cpghRep' + row + '.sp1_0').setVisible(!!chi);
                $('cpghRep' + row + '.sp1_1').setVisible(!!peng);
                $('cpghRep' + row + '.sp1_2').setVisible(!!gang);
                $('cpghRep' + row + '.sp1_3').setVisible(!!hu);
                $('cpghRep' + row + '.sp1_4').setVisible(true);
                $('cpghRep' + row + '.sp1_5').setVisible(!!ting);

                $('cpghRep' + row + '.sp_0').setVisible(!chi);
                $('cpghRep' + row + '.sp_1').setVisible(!peng);
                $('cpghRep' + row + '.sp_2').setVisible(!gang);
                $('cpghRep' + row + '.sp_3').setVisible(!hu);
                $('cpghRep' + row + '.sp_4').setVisible(false);
                $('cpghRep' + row + '.sp_5').setVisible(!ting);

                var pai = $('cpghRep' + row + '.sp_pai');
                var paiName = getPaiNameByRowAndId(2, paiId, true);
                pai.setScale(0.65, 0.65);
                setSpriteFrameByName(pai, paiName, that.getPaiPlist());
                cpg.setVisible(true);
            }
        },
        showTingLiang: function (row, ting, liang) {
            return;
            var that = this;

            if (!isReplay) {
                $('tl.ting').setVisible(!!ting);
                $('tl.liang').setVisible(!!liang);

                ting && TouchUtil.setOnclickListener($('tl.ting'), function () {
                    that.sendChiPengGang(OP_PENG, 2, paiId);
                    that.hideChiPengGangHu();
                });

                liang && TouchUtil.setOnclickListener($('tl.liang'), function () {
                    that.sendChiPengGang(OP_PENG, 2, paiId);
                    that.hideChiPengGangHu();
                });

                $('tl').setVisible(true);
            } else {
                var cpg = $('cpghRep' + row);
                for (var i = 0; i < 6; i++) {
                    if (!cpg.getChildByName("sp1_" + i)) {
                        var sp = new cc.Sprite(res['sp1_' + i]);
                        sp.setPosition(cpg.getChildByName("sp_" + i).getPosition());
                        sp.setName("sp1_" + i);
                        cpg.addChild(sp);
                        sp.setVisible(false);
                    }
                }
                $('cpghRep' + row + '.sp1_0').setVisible(!!chi);
                $('cpghRep' + row + '.sp1_1').setVisible(!!peng);
                $('cpghRep' + row + '.sp1_2').setVisible(!!gang);
                $('cpghRep' + row + '.sp1_3').setVisible(!!hu);
                $('cpghRep' + row + '.sp1_4').setVisible(true);
                $('cpghRep' + row + '.sp1_5').setVisible(false);

                $('cpghRep' + row + '.sp_0').setVisible(!chi);
                $('cpghRep' + row + '.sp_1').setVisible(!peng);
                $('cpghRep' + row + '.sp_2').setVisible(!gang);
                $('cpghRep' + row + '.sp_3').setVisible(!hu);
                $('cpghRep' + row + '.sp_4').setVisible(false);
                $('cpghRep' + row + '.sp_5').setVisible(true);

                var pai = $('cpghRep' + row + '.sp_pai');
                var paiName = getPaiNameByRowAndId(2, paiId, true);
                pai.setScale(0.65, 0.65);
                setSpriteFrameByName(pai, paiName, that.getPaiPlist());
                cpg.setVisible(true);
            }
        },
        hideChiPengGangHu: function () {
            // cc.error('1');
            $('cpghg').setVisible(false);
            if ($('chiLayer'))
                $('chiLayer').removeFromParent(false);
        },
        sendChiPengGang: function (op, row, paiId, oriPaiId) {
            network.send(4003, {room_id: gameData.roomId, op: op, pai_id: paiId, ori_pai_id: oriPaiId});
        },
        sendChiPengGangForLisi: function (op, row, paiId, lisiCnt) {
            network.send(4003, {room_id: gameData.roomId, op: op, pai_id: paiId, peng_pai_Cnt: lisiCnt});
        },
        sendPengForLLFY: function (op, row, paiId, lisiCnt) {
            network.send(4003, {room_id: gameData.roomId, op: op, pai_id: paiId, peng_pai_Cnt: lisiCnt});
        },
        sendGangForLLFY: function (op, row, paiId, lisiCnt) {
            network.send(4003, {room_id: gameData.roomId, op: op, pai_id: paiId, gang_pai_Cnt: lisiCnt});
        },
        playChiPengGangHuAnim: function (row, op, isZimo, isAngang) {
            var ccsname = isZimo ? 'zimo_json' : {
                1: 'CHI_json',
                2: 'PENG_json',
                3: 'MINGGANG_json',
                4: 'HU_json',
                6: 'ting_json',
                7: 'liang_json',
            }[op];

            var sex = position2sex[row];
            if(isZimo||op==4)
            {
                if(row==2)
                {
                    this.playPaiHuoEffect(row);
                }
            }

            var infoNode = $('info_n' + row);
            if (infoNode.children.length != 0) {
                infoNode.removeAllChildren();
            }

            var dongHuaName=res[ccsname];
            if(isAngang) {
                dongHuaName=res['ANGANG_json']
            }
            var node= playAnimScene(infoNode, dongHuaName, 0, 0, false, function () {
                infoNode.removeAllChildren();
            });
            // var node= playAnimSceneCall(infoNode, dongHuaName, 0, 0, false,function () {
            //     infoNode.removeAllChildren();
            // });


            if (row==2 && op==16)
            {
                node.runAction(cc.sequence(
                    cc.delayTime(0.9),
                    cc.moveTo(0.3,
                        -500,
                        -10),

                    cc.callFunc(function () {
                        node.removeFromParent();
                    })
                ))
            }
        },
        addCustomListener: function (type, func) {
            this.addListener(type, func);
        },
        addListener:function(code, listener){
            network.addListener(code, listener, this);
        },
        setReplayProgress: function (cur, total) {
            var progress = cur / total * 100;
            this.showTip("回放进度: " + progress.toFixed(1) + '%', false);
        },

        //播放牌 着火 特效
        playPaiHuoEffect: function (row) {
            return;
            var that = this;
            var interval;
            var idx = 0;

            //一个个删除牌火特效
            var closePaiEffect = function () {
                if (idx >= 14) {
                    clearInterval(interval);
                    return;
                }
                var pai = that.getPai(row, idx);
                if (pai && pai.isVisible() && pai.getChildByName('effect')) {
                    pai.getChildByName('effect').removeFromParent();
                }
                idx++;
            };

            var delayFun = function () {
                clearInterval(interval);
                interval = setInterval(closePaiEffect, 30);
                idx = 0;
            };

            //延迟 牌 一个一个的出现特效
            var playPaiEffect = function () {
                if (idx >= 14) {
                    clearInterval(interval);
                    //延迟等待...让牌火特效多停留一阵
                    interval = setInterval(delayFun, 600);
                    idx = 0;
                    return;
                }
                // console.log("生成牌火特效:" + idx);
                var pai = that.getPai(row, idx);
                if (pai && pai.isVisible()) {
                    var effect = playAnimScene(pai, res['HEIMO2_json'], 0, 0, true);
                    effect.setName('effect');
                    effect.x = 44;
                    effect.y = 60;
                }
                idx++;
            };
            interval = setInterval(playPaiEffect, 20);
        },

        setAllPai4Replay: function (data) {
            for (var uid in data)
                if (data.hasOwnProperty(uid)) {
                    var row = uid2position[uid];
                    var paiArr = data[uid]['pai_arr'];
                    this.setLisiByRow(row, data[uid]);
                    this.setPaiArrOfRow(row, paiArr, (row != 2), false);

                    var duiArr = data[uid]['dui_arr'];
                    for (var j = 0; j < duiArr.length; j++) {
                        var dui = duiArr[j];
                        if (dui.type == 1) this.setGChi(row, j, dui['pai_arr']);
                        if (dui.type == 2) this.setGPeng(row, j, dui['pai_arr'][0]);
                        if (dui.type == 3) this.setGGang(row, j, dui['pai_arr'][0], dui['pai_arr'][0]);
                        if (dui.type == 4) this.setGGang(row, j, dui['pai_arr'][0], 0);
                        if (dui.type == 5) this.setGHide(row, j);
                    }
                    for (; j < 4 && $('row' + row + '.g' + j); j++)
                        $('row' + row + '.g' + j).setVisible(false);

                    if ($('row' + row + '.g' + 0).isVisible() == 0) {
                        $('row' + row + '.a' + 0).setPosition(posConf.paiA0PosBak[row]);
                    }

                    var usedArr = data[uid]['used_arr'];
                    for (var j = 0; j < 24; j++) {
                        var pai = $('row' + row + '.c0.b' + j);
                        if (pai)
                            pai.setVisible(false);
                        else
                            break;
                    }
                    for (var j = 0; j < usedArr.length; j++) {
                        this.addUsedPai(row, usedArr[j])
                    }

                    this.recalcPos(row);
                }
        },
        chiPengGangHu: function (op, row, paiId, fromRow, data) {
            if (isReplay) {
                var node = $('cpghRep' + row + '.sp_' + (op - 1));

                var touch_node = new cc.Node();
                $('cpghRep' + row).addChild(touch_node);
                touch_node.setPosition(node.getPositionX(), node.getPositionY() - 40);

                var touch_sp0 = new cc.Sprite(res.touch0);
                var touch_sp1 = new cc.Sprite(res.touch1);
                touch_node.addChild(touch_sp0);
                touch_node.addChild(touch_sp1);
                touch_sp1.setVisible(false);

                touch_node.runAction(cc.sequence(cc.delayTime(0.5), cc.callFunc(function () {
                    touch_sp0.setVisible(false);
                    touch_sp1.setVisible(true);
                }), cc.delayTime(0.818), cc.callFunc(function () {
                    touch_node.removeFromParent();
                    $('cpghRep' + row).setVisible(false);
                })));
            }

            var j;
            for (j = 0; j < 4; j++) {
                var g = $('row' + row + '.g' + j);
                if (!g || !$('row' + row + '.g' + j).isVisible())
                    break;
            }

            var paiArr = this.getPaiArr();

            if (op == OP_CHI) {
                var oriPaiId = data['ori_pai_id'];
                this.setGChi(row, j, paiId, oriPaiId);
                hasChupai = false;
                this.removeOneTopUsedPai(fromRow);
                this.playChiPengGangHuAnim(row, op);
                playEffect('vchi', position2sex[row]);
            } else if (op == OP_PENG || op == OP_GANG) {
                var duiArr = (data['dui_arr'] || []);
                var isJiagang = (data['is_jiagang'] || 0);


                if (op == OP_PENG) {

                    if (mapId == MAP_ID_SHANXI_LLFY) {
                        var duiArr = (data['player']['dui_arr'] || []);
                        this.setDuiArr(row, duiArr);
                    }
                    else {
                        this.setGPeng(row, j, paiId, fromRow);
                    }
                    _.remove(paiArr, function (n) {
                        return n == paiId;
                    });
                    hasChupai = false;
                    this.removeOneTopUsedPai(fromRow);
                    this.playChiPengGangHuAnim(row, op);
                    playEffect('vpeng', position2sex[row]);
                }
                else if (op == OP_PENG || op == OP_GANG) {
                    var isAngang = (row == fromRow && !isJiagang);
                    var upPaiId = paiId;
                    var downPaiId = (isAngang ? 0 : paiId);
                    if (mapId == MAP_ID_SHANXI_LLFY) {
                        if (duiArr && !isJiagang)
                            this.setDuiArr(row, duiArr);
                        else {
                            if (isJiagang) {
                                this.setGJiaGang(row, paiId, fromRow);
                            }
                        }
                    }
                    else {
                        if (duiArr)
                            this.setDuiArr(row, duiArr);
                        else {
                            if (isAngang || !isJiagang) {   // angang or minggang
                                this.setGGang(row, j, upPaiId, downPaiId, fromRow);
                            }
                            else if (isJiagang) {
                                this.setGJiaGang(row, paiId, fromRow);
                            }
                        }
                    }

                    if (row != fromRow)
                        this.removeOneTopUsedPai(fromRow);

                    _.remove(paiArr, function (n) {
                        return n == paiId;
                    });

                    hasChupai = false;
                    if (row == 2 || isJiagang)
                        hasChupai = true;

                    this.playChiPengGangHuAnim(row, op, false, isAngang);
                    playEffect(isAngang ? 'vangang' : 'vgang', position2sex[row]);
                }
                // if(gameData.mapId != MAP_ID_SHANXI_LLFY) {
                this.hideChiPengGangHu();
                // }

                if (!isJiagang && !duiArr)
                    $('row' + row + '.g' + j).setVisible(true);
            }
            else if (op == OP_HU) {
                var isZimo = (data['is_zimo'] || 0);

                if (fromRow == 2)
                    this.hideChiPengGangHu();
                if (!isZimo) {
                    this.removeOneTopUsedPai(fromRow, paiId);
                }
                this.playChiPengGangHuAnim(row, op, isZimo);
                playEffect(isZimo ? 'vzimo' : 'vhu', position2sex[row]);
            }
            else if (op == OP_TING) {
                $('info' + fromRow + '.liang').setVisible(true);
                this.playChiPengGangHuAnim(row, op, isZimo);
                this.hideChiPengGangHu();
                playEffect('vting', position2sex[row]);
            }
            else {
                return;
            }

            this.playerOnloneStatusChange(row, false);

            var _paiArr = (data['pai_arr'] || []);
            var liangPaiArr = (data['liang_pai_arr'] || []);
            var dingArr = (data['dingArr'] || []);
            if (row == 2) {
                gameData.dingArr = liangPaiArr;
            }
            var paiCnt = (data['pai_cnt'] || 0);
            var n = paiCnt - _paiArr.length;
            for (var i = 0; i < n; i++)
                _paiArr.push(0);
            if (op == OP_HU)
                curHuPaiId = paiId;
            if (gameData.mapId == MAP_ID_SHANDONG || gameData.mapId == MAP_ID_LINYI) {
                this.setPaiArrOfRow(row, _paiArr, (row != 2), !data['isTing'], liangPaiArr, false, curHuPaiId);
            }
            else if (gameData.mapId == MAP_ID_SHANXI_LLFY) {
                // if(op!=OP_HU) {
                this.setPaiArrOfRow(row, _paiArr, (row != 2), (op != OP_HU), liangPaiArr, false, curHuPaiId);
                _paiArr = this.sortPaiArrForLisi(_paiArr, liangPaiArr);
                if (row == 2 || isReplay) {
                    var tempPaiArr = _.clone(_paiArr);
                    for (var i = 0; i < paiArr.length; i++) {
                        this.getPai(row, i).userData.isDing = false;
                        this.getPai(row, i).userData.isLiang = true;
                        var lb = this.getPai(row, i).getChildByName('lb');
                        if (lb) {
                            lb.setVisible(false);
                        }
                    }
                    for (var j = 0; j < liangPaiArr.length; j++) {
                        var _index = tempPaiArr.indexOf(liangPaiArr[j]);
                        if (_index != -1) {
                            this.getPai(row, _index).userData.isDing = true;
                            if (data['hasDing']) {
                                this.getPai(row, _index).userData.isLiang = false;
                            }
                            Filter.grayMask(this.getPai(row, _index));
                            var lb = this.getPai(row, _index).getChildByName('lb');
                            if (!lb) {
                                lb = new cc.Sprite(res.lb_liang);
                                lb.setTexture(res.lb_liang);
                                lb.setName('lb');
                                this.getPai(row, _index).addChild(lb);
                                lb.setPosition(17.1, 21.5);
                            }
                            else
                                lb.setTexture(res.lb_liang);
                            lb.setVisible(true);
                            delete tempPaiArr[_index];
                        }
                    }
                }
                // }
            }
            else {
                this.setPaiArrOfRow(row, _paiArr, (row != 2), (op != OP_HU), liangPaiArr, false, curHuPaiId);
            }
            if (op == OP_HU && _.isObject(data['player']) && mapId != MAP_ID_SHANXI_LLFY) {
                var playerMap = data['player'];
                for (var uid in playerMap) {
                    var tRow = uid2position[uid];
                    var tPaiArr = playerMap[uid]['pai_arr'];
                    var tDuiArr = playerMap[uid]['dui_arr'];
                    if (tRow != 2) {
                        for (var i = 0; i < tPaiArr.length; i++)
                            tPaiArr[i] = 0;
                    }
                    this.setDuiArr(tRow, tDuiArr);
                    this.setLisiByRow(tRow, playerMap[uid]);
                    this.setPaiArrOfRow(tRow, tPaiArr, (tRow != 2), (tRow != 2));
                    this.recalcPos(tRow);
                }
            }

            if (op != OP_HU && op != OP_TING) {
                this.setPai(row, 13, 0);
                this.getPai(row, 13).runAction(cc.fadeOut(0));
            }
            this.recalcPos(row);

            if (mapId == MAP_ID_WUHAN_KAIKOU || mapId == MAP_ID_WUHAN_KOUKOU) {
                var multiple = data['multiple'];
                $('info' + row + '.lb_bs').setString('x' + multiple);
            }

            this.checkPaiAmount();
            var that = this;
            if (mapId == MAP_ID_SHANXI_LLFY && op == OP_TING && row == 2) {
                this.hideChiPengGangHu();
                if (data == null || hasChupai) return;
                afterLianging = false;
                var tingpaino = false;
                //胡牌提示
                for (var i = 0; i < 14; i++) {
                    (function (i) {
                        var cardSprite = that.getPai(2, i);
                        var userData = cardSprite.getUserData();
                        var _paiId = userData.pai;
                        for (var cards in data.tishi) {
                            if (data.tishi[cards] == _paiId) {
                                if (!tingpaino && _paiId == paiId) {
                                    cardSprite.userData.isLiang = false;
                                    Filter.grayMask(cardSprite);
                                    tingpaino = true;
                                    break;
                                }
                                cardSprite.userData.isLiang = true;
                                Filter.remove(cardSprite);
                                // hucards = data.tishi[cards];
                                if (cardSprite.userData.isDing && gameData.hasDing) {
                                    cardSprite.userData.isLiang = false;
                                    Filter.grayMask(cardSprite);
                                }
                                break;
                            } else {
                                cardSprite.userData.isLiang = false;
                                Filter.grayMask(cardSprite);
                            }
                        }
                    })(i);
                }
            }
        },
        //hjz
        fapai: function (paiArr, otherDing) {
            var that = this;
            _.pull(paiArr, 0);
            if (mapId == MAP_ID_SHANXI_TAIYUAN) {
                paiArr = this.sortPaiArrForLisi(paiArr);
                this.setLisiCount(0, 4);
                this.setLisiCount(1, 4);
                this.setLisiCount(3, 4);
            }
            else if (mapId == MAP_ID_SHANXI_LLFY) {

            }
            else {
                paiArr.sort(compareTwoNumbers);
            }
            forRows(function (i) {
                if (mapId == MAP_ID_SHANXI_LLFY) {
                    if (i == 2) {
                        for (var k = 0; k < 4; k++) {
                            if (otherDing[k].uid == position2uid[i]) {
                                var dingArr = otherDing[k].dingArr;
                                paiArr = this.sortPaiArrForLisi(paiArr, dingArr);
                                for (var j = 0; j < paiArr.length; j++)
                                    this.setPai(i, j, (i == 2 ? paiArr[j] : 0), (i != 2), (i != 2));
                                for (; j < 14; j++)
                                    this.setPai(i, j, 0, (i != 2), (i != 2), false);
                                break;
                            }
                        }
                        for (var k = 0; k < 6; k++) {
                            var lb = this.getPai(2, k).getChildByName('lb');
                            if (!lb) {
                                lb = new cc.Sprite(res.lb_liang);
                                lb.setTexture(res.lb_liang);
                                lb.setName('lb');
                                this.getPai(2, k).addChild(lb);
                                lb.setPosition(17.1, 21.5);
                            }
                            else
                                lb.setTexture(res.lb_liang);
                            lb.setVisible(true);
                        }

                    }
                    else {
                        for (var k = 0; k < 4; k++) {
                            if (otherDing[k].uid == position2uid[i]) {
                                var dingArr = otherDing[k].dingArr;
                                dingArr.sort(compareTwoNumbers);
                                for (var j = 0; j < dingArr.length; j++)
                                    this.setPai(i, j, dingArr[j], (i != 2), true);
                                for (; j < paiArr.length; j++)
                                    this.setPai(i, j, (i == 2 ? paiArr[j] : 0), (i != 2), (i != 2));
                                for (; j < 14; j++)
                                    this.setPai(i, j, 0, (i != 2), (i != 2), false);
                                break;
                            }
                        }
                    }
                }
                else {
                    for (var j = 0; j < paiArr.length; j++)
                        this.setPai(i, j, (i == 2 ? paiArr[j] : 0), (i != 2), (i != 2));
                    for (; j < 14; j++)
                        this.setPai(i, j, 0, (i != 2), (i != 2), false);
                }


            });
            if (mapId == MAP_ID_SHANXI_LLFY) {
                for (var k = 0; k < 4; k++) {
                    if (otherDing[k].uid == position2uid[2]) {
                        var dingArr = otherDing[k].dingArr;
                        var tempPaiArr = _.clone(paiArr);
                        for (var i = 0; i < paiArr.length; i++) {
                            this.getPai(2, i).userData.isDing = false;

                        }
                        for (var j = 0; j < dingArr.length; j++) {
                            var _index = tempPaiArr.indexOf(dingArr[j]);
                            if (_index != -1) {
                                this.getPai(2, _index).userData.isDing = true;
                                delete tempPaiArr[_index];
                            }

                        }
                    }
                    var row = uid2position[otherDing[k].uid];
                    var dingArr = otherDing[k].dingArr;
                    dingArr.sort(compareTwoNumbers);
                    if (!isReplay) {
                        for (var i = 0; i < 8; i++) {
                            $("dingPanel.mingpai_" + row + ".b" + i).setVisible(false);
                        }
                        for (var i = 0; i < dingArr.length; i++) {
                            var paiName = getPaiNameByRowAndId(row, dingArr[i], false, false);
                            $("dingPanel.mingpai_" + row + ".b" + i).setVisible(true);
                            setSpriteFrameByName($("dingPanel.mingpai_" + row + ".b" + i), paiName,that.getPaiPlist());
                        }
                    }
                }
            }
            this.setPai(2, 13, 0, false, false, this.isMyTurn());

            forRows(function (i) {
                for (var j = 0; j < 13; j++) {
                    this.getPai(i, j).setVisible(true);
                    this.getPai(i, j).runAction(
                        cc.sequence(cc.delayTime(j * FAPAI_ANIM_DELAY), cc.fadeIn(FAPAI_ANIM_DURATION))
                    );
                    var userData = this.getPai(i, j).getUserData();
                    userData.isUp = false;
                    userData.isUpping = false;
                    userData.isDowning = false;
                }
                this.getPai(i, 13).setVisible(false);
                this.getPai(i, 13).getUserData().isUp = false;
                this.getPai(i, 13).isUp = false;
                this.getPai(i, 13).isUpping = false;
                this.getPai(i, 13).isDowning = false;
                this.recalcPos(i);
            });
            this.enableChuPai();
            this.liangAllPai();

            if ($('info0.liang')) $('info0.liang').setVisible(false);
            if ($('info1.liang')) $('info1.liang').setVisible(false);
            if ($('info2.liang')) $('info2.liang').setVisible(false);
            if ($('info3.liang')) $('info3.liang').setVisible(false);
            hideGangStep = 0;
            liangStep = 0;
            afterLianging = false;

            curHuPaiId = 0;

            if (mapId == MAP_ID_SHANXI_WEINAN) {
                this.quePaiGray();
            }
        },

        jiesuan: function (data) {
            var that = this;

            var myScore = 0;
            var players = data.players;
            for (var i = 0; i < players.length; i++) {
                var player = players[i];
                var uid = player['uid'];

                var scoreEnd = 0;
                if (mapId == MAP_ID_SHANXI_DADIANZI) {
                    scoreEnd = 0;
                } else if (mapId == MAP_ID_SHANXI_JINGBIAN) {
                    scoreEnd = 0;
                }
                else if (mapId == MAP_ID_SHANXI_LLFY) {
                    scoreEnd = 0;
                }

                gameData.players[position2playerArrIdx[uid2position[uid]]].score = player['total_score'] - scoreEnd;
                $('info' + uid2position[uid] + '.lb_score').setString(player['total_score']);

                if (uid == gameData.uid) {
                    myScore = player['score'];
                    continue;
                }

                var row = uid2position[uid];
                var paiArr = player['pai_arr'];

                if (data['hu_type'] == 2 && player['hu'])
                    this.setPaiArrOfRow(uid2position[uid], paiArr, (row != 2), false, [], false, curHuPaiId);
                else {
                    this.setPaiArrOfRow(uid2position[uid], paiArr, (row != 2), false);
                }
                that.recalcPos(row);
            }

            setTimeout(function () {
                var layer = new JiesuanLayer(data);
                that.addChild(layer,jiesuanZorder);

                playEffect(myScore >= 0 ? 'vWin' : 'vLose');
            }, 2000);
        },
        onJiesuanClose: function (isReady) {
            if (isReplay)
                return;
            if (isReady) {
                $('btn_zhunbei').setVisible(false);
            } else {
                $('btn_zhunbei').setVisible(true);
            }
        },
        zongJiesuan: function (data) {
            var that = this;
            setTimeout(function () {
                var layer = new ZongJiesuanLayer(data);
                that.addChild(layer,zongjiesuanZorder);
            }, 2000);
        },

        playerOnloneStatusChange: function (row, isOffline) {
            var offline = $('info' + row + '.offline');
            if (offline) {
                offline.setVisible(!!isOffline);
            }
        },

        setZhuang: function (row) {
            var that = this;
            $('info0.zhuang').setVisible(false);
            $('info1.zhuang').setVisible(false);
            $('info2.zhuang').setVisible(false);
            $('info3.zhuang').setVisible(false);
            if(row || row == 0)
                $('info' + row + '.zhuang').setVisible(true);
            that.setDirection(gameData.players, true);
        },

        setDirection: function (players, isVisible) {
            return ;
            for(var i=0; i< players.length; i++){
                $('timer.dir_' + uid2position[players[i].uid]).setString(direction[players[i].pos]);
                $('timer.dir_' + uid2position[players[i].uid]).setVisible(isVisible);
            }
        },

        setLianZhuang: function (lianzhuang) {
            $('lianzhuang').setVisible(true);
            $('lianzhuang').setString("庄 X " + lianzhuang);
        },
        setBeforeOnCCSLoadFinish: function (cb) {
            this.beforeOnCCSLoadFinish = cb;
        },
        getBeforeOnCCSLoadFinish: function (cb) {
            return this.beforeOnCCSLoadFinish;
        },
        setAfterGameStart: function (cb) {
            this.afterGameStart = cb;
        },
        setReady: function (uid) {
            var ok = $('info' + uid2position[uid] + '.ok');
            ok.setVisible(true);
            if (uid == gameData.uid)
                $('btn_zhunbei').setVisible(false);

            if (uid2playerInfo[uid]) {
                uid2playerInfo[uid]['ready'] = 1;
            }
            this.refreshPlayersStates();

        },

        refreshPlayersStates: function () {
            if (this.assistantLayer && cc.sys.isObjectValid(this.assistantLayer)) {
                this.assistantLayer.refreshPlayersStates();
            }
        },

        showToast: function (msg) {
            var toast = $('toast');
            if (!toast) {
                toast = new cc.Sprite(res.toast_bg_png);
                toast.setName("toast");
                this.addChild(toast);

                var text = new ccui.Text();
                text.setName('text');
                text.setFontSize(30);
                text.setTextColor(cc.color(255, 255, 255));
                text.setPosition(toast.getBoundingBox().width / 2, toast.getBoundingBox().height / 2);
                text.setString(msg);
                toast.setPosition(cc.winSize.width / 2, cc.winSize.height / 2 * 4 / 5);
                toast.addChild(text);
            }
            toast.stopAllActions();
            toast.runAction(cc.sequence(cc.fadeIn(3), cc.fadeOut(0.3)));
            text = toast.getChildByName('text');
            text.runAction(cc.sequence(cc.fadeIn(3), cc.fadeOut(0.3)));
        },

        showArrow: function (pai) {
            this.hideArrow();
            var arrow = pai.getParent().getChildByName('arrow');
            if (!arrow) {
                arrow = new cc.Sprite(res.arrow);
                arrow.setAnchorPoint(0.5, 0);
                arrow.setName('arrow');
                arrow.setLocalZOrder(100);
                pai.getParent().addChild(arrow);
                var duration = 0.8;
                arrow.setOpacity(168);
            }
            arrow.setVisible(true);
            arrow.setPositionX(pai.getPositionX());
            arrow.setPositionY(pai.getPositionY() + 10);
            arrow.runAction(cc.sequence(cc.spawn(cc.moveBy(duration, 0, +16), cc.fadeTo(duration, 255))
                , cc.spawn(cc.moveBy(duration, 0, -16), cc.fadeTo(duration, 168))).repeatForever());
        },

        changeBtnStatus: function () {
            $('btn_bg').setVisible(!$('btn_bg').isVisible());
            $('setting').setVisible(!$('setting').isVisible());
            $('btn_changeTable').setVisible(!$('btn_changeTable').isVisible());
            $('location_btn_red').setVisible(!$('location_btn_red').isVisible());
            // var isVisible = $('chat').isVisible();
            // $('chat').setVisible(!$('chat').isVisible());
            $('jiesan').setVisible(!$('jiesan').isVisible());
            $('btn_control_btns').setFlippedY(!$('btn_control_btns').isFlippedY());
        },
        hideControlBtns: function () {
            $('btn_bg').setVisible(false);
            $('setting').setVisible(false);
            // $('chat').setVisible(false);
            $('jiesan').setVisible(false);
            $('btn_changeTable').setVisible(false);
            $('location_btn_red').setVisible(false);
            $('btn_control_btns').setFlippedY(true);
        },

        setLiang: function (liang) {
            lianging = liang;
            return;
            var that = this;
            if (liang) {
                lianging = true;
                for (var i = 0; i < 14; i++) {
                    var pai = that.getPai(2, i);
                    var userData = pai.getUserData();
                    if (userData.pai) {
                        userData.isLiang = false;
                        Filter.grayMask(pai);
                    }
                }
                $('btn_queding').setVisible(true);
                $('btn_quxiao').setVisible(true);
            }
            else {
                $('btn_queding').setVisible(false);
                $('btn_quxiao').setVisible(false);
                that.hideTip();
                lianging = false;
            }
        },
        showTip: function (content, isShake) {
            isShake = _.isUndefined(isShake) ? true : isShake;
            var scale9sprite = $('top_tip');
            if (!(scale9sprite instanceof cc.Scale9Sprite)) {
                var newScale9sprite = new cc.Scale9Sprite(res.round_rect_157, cc.rect(0, 0, 91, 32), cc.rect(46, 16, 1, 1));
                newScale9sprite.setName('top_tip');
                scale9sprite.setAnchorPoint(0.5, 0.5);
                newScale9sprite.setPosition(scale9sprite.getPosition());
                scale9sprite.getParent().addChild(newScale9sprite);
                var lb = $('top_tip.lb_tip');

                text = new ccui.Text();
                text.setName('lb_tip');
                text.setFontSize(lb.getFontSize());
                text.setTextColor(lb.getTextColor());
                text.enableOutline(cc.color(38, 38, 38), 1);
                newScale9sprite.addChild(text);

                lb.removeFromParent(true);
                scale9sprite.removeFromParent(true);
                scale9sprite = newScale9sprite;
            }
            var text = $('top_tip.lb_tip');
            text.setString(content);
            var size = cc.size(text.getVirtualRendererSize().width + 35, scale9sprite.getContentSize().height);
            text.setPosition((text.getVirtualRendererSize().width + 35) / 2, scale9sprite.getContentSize().height / 2);
            scale9sprite.setContentSize(size);
            scale9sprite.setScale(1.2);
            scale9sprite.setVisible(true);
            if (isShake)
                scale9sprite.runAction(cc.sequence(cc.scaleTo(0.2, 1.4), cc.scaleTo(0.2, 1.2)));
        },
        hideTip: function () {
            if (!isReplay)
                $('top_tip').setVisible(false);
        },
        hideArrow: function () {
            for (var i = 0; i < 4; i++) {
                var arrow = $('row' + i + '.c0.arrow');
                if (arrow)
                    arrow.removeFromParent(true);
            }
        },
        liangAllPai: function () {
            for (var i = 0; i < 14; i++) {
                var pai = this.getPai(2, i);
                var userData = pai.getUserData();
                if (userData.pai) {
                    userData.isLiang = true;
                    Filter.remove(pai);
                }
            }
        },
        selectHideGangCb: function () {
            network.send(4015, {room_id: gameData.roomId, hided_gang_arr: hidedGangArr});
        },
        throwDice: function (title, dice, paiIdA, paiIdB, cb) {
            var that = this;
            title = title || '';
            if (dice < 1 || dice > 6)
                return;
            var layer = that.throwDiceLayer;
            if (!layer) {
                // var scene = ccs.load(res.ThrowDice_json);
                var scene = loadNodeCCS(res.ThrowDice_json, that);
                layer = scene.node;
                layer.setName('throwDice');
                layer.retain();
                that.throwDiceLayer = layer;
            }
            var paiA = $('root.a0', layer);
            var paiB = $('root.a1', layer);
            paiA.setVisible(false);
            paiB.setVisible(false);
            that.addChild(layer);
            $('root.lb_title', layer).setString(title);
            playThrowDice($('root.dice1', layer), Math.floor(Math.random() * 6) + 1);
            playThrowDice($('root.dice', layer), dice, function () {
                var paiAName = getPaiNameByRowAndId(2, paiIdA, true, false);
                var paiBName = getPaiNameByRowAndId(2, paiIdB, true, false);
                setSpriteFrameByName(paiA, paiAName, that.getPaiPlist());
                setSpriteFrameByName(paiB, paiBName, that.getPaiPlist());
                paiA.setVisible(paiIdA > 0);
                paiB.setVisible(paiIdB > 0);
                if (paiIdB == 0)
                    paiA.setPositionX(paiA.getParent().getContentSize().width / 2);
                setTimeout(function () {
                    layer.removeFromParent(false);
                    if (cb)
                        cb();
                }, 2600);
            });
        },
        showChiLayer: function (paiId, cb) {
            var that = this;
            var paiArr = that.getPaiArr();
            paiArr.push(paiId);
            var arr = [
                [paiId - 2, paiId - 1, paiId],
                [paiId - 1, paiId, paiId + 1],
                [paiId, paiId + 1, paiId + 2]
            ];
            var posibleIdxArr = [];
            for (var i = 0; i < arr.length; i++) {
                var a = arr[i][0];
                var b = arr[i][1];
                var c = arr[i][2];
                if (isSameColor(a, b) && isSameColor(a, c) &&
                    paiArr.indexOf(a) >= 0 && paiArr.indexOf(b) >= 0 && paiArr.indexOf(c) >= 0)
                    posibleIdxArr.push(i);
            }
            if (posibleIdxArr.length == 1) {
                cb(arr[posibleIdxArr[0]][1], paiId);
                that.hideChiPengGangHu();
            }
            else if (posibleIdxArr.length > 1) {
                var layer = that.chiLayer;
                if (!layer) {
                    var scene = loadNodeCC(res.ChiPanel_json, that);
                    layer = scene.node;
                    layer.setName('chiLayer');
                    layer.retain();
                    that.chiLayer = layer;
                }
                that.addChild(layer);

                $('root.g0', layer).setVisible(posibleIdxArr.length >= 1);
                $('root.g1', layer).setVisible(posibleIdxArr.length >= 2);
                $('root.g2', layer).setVisible(posibleIdxArr.length >= 3);

                var fun = function (node) {
                    var parentName = node.getParent().getName();
                    var userData = $('root.' + parentName + '.' + 1, layer).getUserData();
                    var _paiId = userData.pai;
                    var paiId = userData.ori_pai_id;
                    cb(_paiId, paiId);
                    that.hideChiPengGangHu();
                    layer.removeFromParent(false);
                };

                for (var i = 0; i < posibleIdxArr.length; i++) {
                    for (var j = 0; j < 3; j++) {
                        var pai = $('root.g' + i + '.' + j, layer);
                        var _paiId = arr[posibleIdxArr[i]][j];
                        var paiName = getPaiNameByRowAndId(2, _paiId, true, false);
                        pai.setUserData({pai: _paiId, ori_pai_id: paiId});
                        setSpriteFrameByName(pai, paiName, that.getPaiPlist());

                        TouchUtil.setOnclickListener(pai, fun);
                    }
                }

                TouchUtil.setOnclickListener($('root.btn_close', layer), function () {
                    layer.removeFromParent();
                });
                TouchUtil.setOnclickListener($('root', layer), function () {
                });
            }
        },
        playEffect: function (filename, sex) {
            playEffect(filename, sex);
        },
        HuCardTip: function (showdata, posx, isVisible) {
            var that =this ;
            var hupaitip = maLayer.getChildByName('hupaitip');
            if (!isVisible && hupaitip) {
                hupaitip.setVisible(isVisible);
                return;
            }
            if (showdata == null) {
                for (var i = 0; i < 14; i++) {
                    var pai = this.getPai(2, i);
                    var userData = pai.getUserData();
                    userData.hucards = null;
                }
                return;
            }
            var tiplength = showdata.length;
            if (hupaitip) {
                hupaitip.setVisible(true);
                hupaitip.setContentSize(cc.size(120 + 120 * tiplength, 80));
                //设置位置
                posx = posx + 40;
                if (posx + hupaitip.getContentSize().width / 2 > 1280) {
                    posx = 1280 - hupaitip.getContentSize().width / 2 - 20;
                } else if (posx - hupaitip.getContentSize().width / 2 < 0) {
                    posx = hupaitip.getContentSize().width / 2 + 20;
                }
                hupaitip.setPositionX(posx);
                for (var i = 0; i < 9; i++) {
                    var card = hupaitip.getChildByName('card' + (i + 1));
                    var fantext = hupaitip.getChildByName('fantext' + (i + 1));
                    var fannum = hupaitip.getChildByName('fannum' + (i + 1));
                    var zhangtext = hupaitip.getChildByName('zhangtext' + (i + 1));
                    var zhangnum = hupaitip.getChildByName('zhangnum' + (i + 1));
                    if (tiplength > i) {
                        //card
                        card.setVisible(true);
                        var paiName = getPaiNameByRowAndId(2, showdata[i][0], true, true);
                        setSpriteFrameByName(card, paiName, that.getPaiPlist());
                        card.setPosition(cc.p(-5 + 120 * (i + 1), hupaitip.getContentSize().height / 2));
                        fantext.setVisible(true);
                        fantext.setPosition(cc.p(60 + 120 * (i + 1), 60));
                        fannum.setVisible(true);
                        fannum.setPosition(cc.p(40 + 120 * (i + 1), 60));
                        fannum.setString(showdata[i][1]);
                        zhangtext.setVisible(true);
                        zhangtext.setPosition(cc.p(60 + 120 * (i + 1), 20));
                        zhangnum.setVisible(true);
                        zhangnum.setString(showdata[i][2]);
                        zhangnum.setPosition(cc.p(40 + 120 * (i + 1), 20));
                    } else {
                        card.setVisible(false);
                        fantext.setVisible(false);
                        fannum.setVisible(false);
                        zhangtext.setVisible(false);
                        zhangnum.setVisible(false);
                    }
                }
            } else {
                var hupaitip = new cc.Scale9Sprite(res.toast_bg_png, null, cc.rect(10, 10, 10, 10));
                hupaitip.setName('hupaitip');
                hupaitip.setAnchorPoint(cc.p(0.5, 0));
                hupaitip.setContentSize(cc.size(120 + 120 * tiplength, 80));
                hupaitip.setPosition(cc.p(1280 - 20, 220));
                maLayer.addChild(hupaitip);
                //设置位置
                posx = posx + 40;
                if (posx + hupaitip.getContentSize().width / 2 > 1280) {
                    posx = 1280 - hupaitip.getContentSize().width / 2 - 20;
                } else if (posx - hupaitip.getContentSize().width / 2 < 0) {
                    posx = hupaitip.getContentSize().width / 2 + 20;
                }
                hupaitip.setPositionX(posx);

                var huSprite = new cc.Sprite(res.hupaitip_hu);
                huSprite.setPosition(cc.p(40, hupaitip.getContentSize().height / 2));
                hupaitip.addChild(huSprite);
                for (var i = 0; i < 9; i++) {
                    //card
                    var card = new cc.Sprite();
                    card.setScale(0.88);
                    card.setName('card' + (i + 1));
                    card.setPosition(cc.p(-5 + 120 * (i + 1), hupaitip.getContentSize().height / 2));
                    hupaitip.addChild(card);

                    var fantext = new ccui.Text();
                    fantext.setString('番');
                    fantext.setName('fantext' + (i + 1));
                    fantext.setFontSize(20);
                    fantext.setTextColor(cc.color(0, 255, 0));
                    fantext.enableOutline(cc.color(38, 38, 38), 1);
                    fantext.setPosition(cc.p(60 + 120 * (i + 1), 60));
                    hupaitip.addChild(fantext);
                    var fannum = new ccui.Text();
                    fannum.setName('fannum' + (i + 1));
                    fannum.setFontSize(20);
                    fannum.setTextColor(cc.color(255, 255, 0));
                    fannum.enableOutline(cc.color(38, 38, 38), 1);
                    fannum.setPosition(cc.p(40 + 120 * (i + 1), 60));
                    hupaitip.addChild(fannum);
                    var zhangtext = new ccui.Text();
                    zhangtext.setString('张');
                    zhangtext.setName('zhangtext' + (i + 1));
                    zhangtext.setFontSize(20);
                    zhangtext.setTextColor(cc.color(0, 255, 0));
                    zhangtext.enableOutline(cc.color(38, 38, 38), 1);
                    zhangtext.setPosition(cc.p(60 + 120 * (i + 1), 20));
                    hupaitip.addChild(zhangtext);
                    var zhangnum = new ccui.Text();
                    zhangnum.setName('zhangnum' + (i + 1));
                    zhangnum.setFontSize(20);
                    zhangnum.setTextColor(cc.color(255, 255, 0));
                    zhangnum.enableOutline(cc.color(38, 38, 38), 1);
                    zhangnum.setPosition(cc.p(40 + 120 * (i + 1), 20));
                    hupaitip.addChild(zhangnum);
                    if (tiplength > i) {
                        card.setVisible(true);
                        var paiName = getPaiNameByRowAndId(2, showdata[i][0], true, true);
                        setSpriteFrameByName(card, paiName,that.getPaiPlist());
                        fantext.setVisible(true);
                        fannum.setVisible(true);
                        fannum.setString(showdata[i][1]);
                        zhangtext.setVisible(true);
                        zhangnum.setVisible(true);
                        zhangnum.setString(showdata[i][2]);
                    } else {
                        card.setVisible(false);
                        fantext.setVisible(false);
                        fannum.setVisible(false);
                        zhangtext.setVisible(false);
                        zhangnum.setVisible(false);
                    }
                }
            }
        },
        setHuTipVisible: function (visible) {
            for (var i = 0; i < 14; i++) {
                var cardSprite = this.getPai(2, i);
                if (cardSprite.getChildByName('hucardSprite')) {
                    cardSprite.getChildByName('hucardSprite').stopAllActions();
                    cardSprite.getChildByName('hucardSprite').setVisible(visible);
                }
            }
        },
        showXuanPao: function () {
            // if (gameData.appId == APP_ID_SHAN3XI || gameData.appId == APP_ID_HEBEI) {
            //     var xuanpao_back = $('xuanpao_back');
            //     xuanpao_back.setVisible(true);
            //     for (var i = 0; i < 5; i++) {
            //         (function (i) {
            //             TouchUtil.setOnclickListener($('xuanpao_back.xuanpao_' + i), function () {
            //                 network.send(4082, {room_id: gameData.roomId, score: i});
            //                 $('xuanpao_back.xuanpao_'+i+'.paodi_1').setVisible(true);
            //                 xuanpao_back.runAction(cc.sequence(
            //                     cc.delayTime(0.3)
            //                     , cc.callFunc(function () {
            //                         $('xuanpao_back.xuanpao_'+i+'.paodi_1').setVisible(false);
            //                         xuanpao_back.setVisible(false);
            //                     })
            //                 ));
            //             });
            //         })(i);
            //     }
            // }
        },
        showPlayerPao: function (p_score) {
            for (var i = 0; i < p_score.length; i++) {
                var uid = p_score[i].uid;
                var score = p_score[i].score;
                var row = uid2position[uid];
                var paoNode = $('info' + row + ".pao");
                paoNode.removeAllChildren();
                $('info' + row + ".isxuanpao").removeAllChildren();
                var xiaopao = new ccui.Text('下炮数:'+ score);
                xiaopao.setFontSize(20);
                xiaopao.setColor(cc.color(247, 226, 131));
                xiaopao.setAnchorPoint(cc.p(0, 0));
                paoNode.addChild(xiaopao);
            }
        },
        hideAllPlayerPao: function () {
            for (var i = 0; i < 4; i++) {
                var paoNode = $('info' + i + ".pao");
                paoNode.removeAllChildren();
            }
        },
        setLisiCount: function (row, count) {
            if (mapId == MAP_ID_SHANXI_TAIYUAN) {
                if (typeof lisiPaiCountList[row] === 'number' && typeof count === 'number') {
                    lisiPaiCountList[row] = count;
                }
            }
        },
        setLisiInfo: function (data, row) {
            if (mapId == MAP_ID_SHANXI_TAIYUAN) {
                row = _.isUndefined(row) ? 2 : row;
                if (typeof data.lisiArr === 'object') {
                    lisiPaiArr[row] = data.lisiArr;
                    this.setLisiCount(row, lisiPaiArr[row].length);
                } else if (typeof data.lisi_arr === 'object') {
                    lisiPaiArr[row] = data.lisi_arr;
                    this.setLisiCount(row, lisiPaiArr[row].length);
                }
            }
        },
        setLisiByRow: function (row, data, isHu) {
            if (row == 2 || isHu) {
                this.setLisiInfo(data, row);
            } else {
                this.setLisiCount(row, data.lisi_pai_cnt);
            }
        },
        sortPaiArrForLisi: function (paiArr, dingArr) {
            var _lisiArr1 = _.clone(dingArr);
            var _lisiArr2 = [];
            for (var k = 0; k < paiArr.length; k++) {
                var _index = _lisiArr1.indexOf(paiArr[k]);
                if (_index == -1) {
                    _lisiArr2.push(paiArr[k]);
                } else {
                    _lisiArr1.splice(_index, 1);
                }
            }
            dingArr.sort(compareTwoNumbers);
            _lisiArr2.sort(compareTwoNumbers);
            return dingArr.concat(_lisiArr2);
        }
        ,
        showQueMen: function () {
            var that = this;
            for (var i in quePaiList) {
                (function (i) {
                    var queInfo = quePaiList[i];
                    if (queInfo && queInfo > 0) {
                        if ($('info' + uid2position[i] + '.que')) {
                            $('info' + uid2position[i] + '.que').setVisible(true);
                            $('info' + uid2position[i] + '.que').setTexture(res["que" + that.getPaiType(queInfo)]);
                        }
                        if (parseInt(i) == gameData.uid) {
                            that.quePaiGray();
                        }
                    } else {
                        if ($('info' + uid2position[i] + '.que')) {
                            $('info' + uid2position[i] + '.que').setVisible(false);
                        }
                    }
                })(i);
            }
        }
        ,
        getPaiType: function (paiId) {
            if (paiId >= 1 && paiId <= 9) {
                return 1;
            } else if (paiId >= 10 && paiId <= 18) {
                return 2;
            } else if (paiId >= 19 && paiId <= 27) {
                return 3;
            } else {
                return 0;
            }
        }
        ,
        quePaiGray: function () {
            var queInfo = quePaiList[gameData.uid];
            if (queInfo && queInfo > 0) {
                var paiType = this.getPaiType(queInfo);
                var pai = null;
                var isGray = false;
                for (var i = 0; i < 14; i++) {
                    pai = this.getPai(2, i);
                    if (pai && pai.userData && pai.userData.pai) {
                        if (this.getPaiType(pai.userData.pai) == paiType) {
                            isGray = true;
                            break;
                        }
                    }
                }
                if (isGray) {
                    for (var j = 0; j < 14; j++) {
                        pai = this.getPai(2, j);
                        if (pai && pai.userData && pai.userData.pai) {
                            if (this.getPaiType(pai.userData.pai) != paiType) {
                                pai.userData.isLiang = false;
                                Filter.grayMask(pai);
                            }
                        }
                    }
                }
            }
        },
        tiAlert: function (destUid) {
            showMessageBox("是否踢出【" + uid2playerInfo[destUid].nickname + "】该玩家？", function () {
                network.send(3010, {room_id: gameData.roomId, dest_uid: destUid});
            }, function () {
            });
        },

        getRowByUid: function (uid) {
            return uid2position[uid];
        },


        isPlayerDisClosed : function () {
            var otherPlayers = [];

            for(var i=0; i <gameData.players.length; i++){
                var playerInfo = gameData.players[i];
                if(gameData.uid == playerInfo['uid']){
                    continue;
                }else {
                    otherPlayers[otherPlayers.length + 1] = playerInfo;
                }
                //其他人和我的距离
                if(gameData.location && playerInfo && playerInfo['loc']){
                    var otherInfo = playerInfo['loc'].split(',');
                    var otherLocation1 = otherInfo[1];
                    var otherLocation2 = otherInfo[0];
                    var mylocation = gameData.location.split(',');
                    var mylocationlat = mylocation[1];
                    var mylocationlng = mylocation[0];
                    var myAndOtherDis = LocationUtil.getFlatternDistance(mylocationlat, mylocationlng, otherLocation1, otherLocation2);
                    if(myAndOtherDis!=undefined){
                        return true;
                    }
                }
            }
            //其他人之间的距离
            var otherPlayerInfo = otherPlayers[0];
            for(var j=1; j< otherPlayers.length; j++){
                var _playerInfo = otherPlayers[j]
                if(otherPlayerInfo && otherPlayers['loc'] && _playerInfo && _playerInfo['loc']){
                    var otherInfo = _playerInfo['loc'].split(',');
                    var otherLocation1 = otherInfo[1];
                    var otherLocation2 = otherInfo[0];
                    var mylocation = otherPlayerInfo.split(',');
                    var mylocationlat = mylocation[1];
                    var mylocationlng = mylocation[0];
                    var myAndOtherDis = LocationUtil.getFlatternDistance(mylocationlat, mylocationlng, otherLocation1, otherLocation2);
                    if(myAndOtherDis!=undefined){
                        return true;
                    }
                }
            }
            return false;
        },

        initDingwei : function () {
            var that = this;
            //定位
            if(that.isPlayerDisClosed()){
                // $('location_btn_red').setVisible(true);
                // $('location_btn_green').setVisible(false);
                //距离过近 自动弹出
                if(gameData.curRound == 0 && !isReplay && gameData.players.length > 2){
                    showWindow('PlayerInfoLocationWindow', false, that);
                }
            }else{
                // $('location_btn_red').setVisible(false);
                // $('location_btn_green').setVisible(true);
            }
        },

        showWanfaLayer: function () {
            if (!gameData.wanfaDesp) {
                return;
            }
            if ($('wanfaPanel').getChildrenCount() > 0) {
                $('wanfaPanel').setVisible(true);
            } else {
                var arr = decodeURIComponent(gameData.wanfaDesp).split(',');
                if (arr.length >= 1)
                    arr = arr.slice(1);
                var wanfaStr = arr.join("\n");
                var text = new ccui.Text(wanfaStr, 'res/fonts/FZCY.ttf', 24);
                var width = text.getContentSize().width + 30;
                var height = text.getContentSize().height + 30;
                text.setPosition(cc.p(width / 2, height / 2));
                var wanfaLayer = new cc.LayerColor(cc.color(0, 0, 0, 189), width, height);
                wanfaLayer.addChild(text);
                wanfaLayer.setPosition(cc.p($('btn_wanfa').x, $('btn_wanfa').y - height));
                $('wanfaPanel').addChild(wanfaLayer,1000);
                $('wanfaPanel').setVisible(true);
            }
        },
        //点击头像添加
        showPlayerInfoPanel: function (idx) {
            if (window.inReview)
                return;
            if (position2playerArrIdx[idx] >= gameData.players.length)
                return;
            var that = this;
            var playerInfo = gameData.players[position2playerArrIdx[idx]];
            network.send(3018, {cmd: 'queryEmoji',uid: gameData.uid,room_id:gameData.roomId});
            showWindow('PlayerInfoInGameWindow',playerInfo,that);
        },
        //获取自己的ChatInfo节点
        getChatInfoNode:function (idx) {
            var self = this;
            var ChatInfoNode = null;
            if($('info' + idx)){
                ChatInfoNode = $('info' + idx + '.ChatInfo');
            }
            return ChatInfoNode;
        },

        //获取初始玩家下表位置
        getOriginalPos: function () {
            return 2;
        },

        //获取道具投放坐标
        getEffectEmojiPos: function (caster, patient, isNotMid) {
            var self = this;
            var pos = {};
            var infoCasterHead = $('info' + caster + '.head');
            var infoPatientHead = $('info' + patient + '.head');
            pos[caster] = infoCasterHead ? infoCasterHead.getParent().convertToWorldSpace(infoCasterHead.getPosition()) : cc.p(cc.winSize.width / 2, cc.winSize.height / 2);
            pos[patient] = (patient != self.getOriginalPos() && infoPatientHead) || isNotMid ? infoPatientHead.getParent().convertToWorldSpace(infoPatientHead.getPosition()) : cc.p(cc.winSize.width / 2, cc.winSize.height / 2);
            return pos;
        },
    });
    exports.MaLayer = MaLayer;
    exports.MaLayer.getPaiNameByRowAndId = getPaiNameByRowAndId;
})
(window);
